Philippe Gioseffi Philippe Gioseffi -4 years ago 94
jQuery Question

How to use unobtrusive javascript when you have parameters coming from back end?

I hava an anchor that nowadays are coded like this:

<a id="link${parameter01}${parameter02}" href="#anchor${parameter03}" onclick="functionName('${parameter01}', '${parameter02}', '${parameter03}'," class="class-name"><i class="fa fa-barcode fa-3x"></i></a>

I know that I can do it in an unbstrusive way by using jQuery with something like below:

$(document).ready(function() {
$('#class-name').on('click', function() {
// Code from functionNameHere.

What I don't know is how to pass the parameters that I have in the obstrusive way. I thought about doing a form with
's hidden but that was rejected because the HTML file would become a lot bigger since this is a list comming from the server.

Answer Source

You can put an attribute on that element and access it with this in your event callback

<div id="foo" data-blah="blah"></div>

$('#foo').on('click', function() {
  const blah = this.getAttribute('data-blah'); // 'blah'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download