JavaScript How to pass arguments to function using unobtrusive JavaScript? pass arguments function python,pass arguments function addeventlistener,pass arguments

In traditional code I will pass arguments to link button like this:

<a target='_blank' href="javascript:call_hello(1, 2, 3)" role="button">Hello</a>

How can I do it in unobtrusive JavaScript style with JQuery or similar library.

<a target='_blank' href="#" class="hello" arg1="1" arg2="2" arg3="3" role="button">Hello</a>

Do not focus that code is longer since it is only example. I want attach Javascript code in such way.

$(".hello").on('click', call_hello2);

Can you suggest the best solution in your opinion for unobtrusive JavaScript style (attaching Javascript by matching elements not by html code).

Answer:1

You should use data-* prefixed custom attributes which can be stored and fetched using $.fn.data()

Store arbitrary data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements.

Alternatively HTMLElement.dataset can also be used.

The HTMLElement.dataset read-only property allows access, both in reading and writing mode, to all the custom data attributes (data-*) set on the element. It is a map of DOMString, one entry for each custom data attribute.

$(function() {
  $('a').on('click', function(event) {
    event.preventDefault();
    var arg1 = $(this).data('arg1');
    
    alert(arg1)
    
    //Using native JS
    console.log(this.dataset.arg1)
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a target='_blank' href="#" class="hello" data-arg1="1" data-arg2="2" data-arg3="3" role="button">Hello</a>
Answer:2

The common approach for arguments passing to a jQuery plugin/helper in an unobtrusive way is to use the data-* attributes:

<a target='_blank' href="#" class="hello" data-arg1="1" data-arg2="2" data-arg3="3" role="button">Hello</a>

Then in your code:

$('.hello').on('click', function(e) {
    var options = $(this).data();

    if (options.arg1 == 1)
        ...
});

You can also set your default options and only override the passed arguments:

$('.hello').on('click', function(e) {
    var defaultOptions = {
         arg1: "something",
         arg2: "something2",
         arg3: "something3"
    },
    passedOptions = $(this).data(),
    options = $.extend(defaultOptions, passedOptions);
});
Answer:3

Should be:

<a id="hello-button" target='_blank' href="#" class="hello" data-args="1,2,3" role="button">Hello</a>

$(".hello").on('click', function (e) {
    var el = $(e.target);
    var args = el.data('args').split(',');
    call_hello(args);
});

This is the most unobtrusive way of doing it. You don't clobber the DOM with lots of attributes nor create arbitrary JavaScript functions.

Answer:4

i am trying to creat a carousel animation that take 3 pics and slide the to the left and bring new 3 images, i don't think i'm in the right direction need help here is the fiddle link: http://...

i am trying to creat a carousel animation that take 3 pics and slide the to the left and bring new 3 images, i don't think i'm in the right direction need help here is the fiddle link: http://...

I have a html string var str = '<span class="linq" onclick="ShowData(2,-1,1);">11111</span> &nbsp;»&nbsp; <span class="linq" onclick="ShowData(16,1,1);">22222</span> &...

I have a html string var str = '<span class="linq" onclick="ShowData(2,-1,1);">11111</span> &nbsp;»&nbsp; <span class="linq" onclick="ShowData(16,1,1);">22222</span> &...

  1. split html string c#
  2. split html string javascript
  3. split html string
  4. split html string jquery
  5. split html string php
  6. html split string into lines
  7. html split string into array
  8. split string html tags
  9. javascript split html string into array
  10. js split html string
  11. python split html string
  12. angular html split string
  13. django html split string
  14. c# split string html tags
  15. php split string html tags
  16. java split string html tags
  17. angularjs html split string
  18. html split long string
  19. html string split function

I have an Angular app where I'm using ui-grid. I want to have a custom action on a cell of the grid that calls a method from my app. So basically, this means calling a method that's somewhere up in ...

I have an Angular app where I'm using ui-grid. I want to have a custom action on a cell of the grid that calls a method from my app. So basically, this means calling a method that's somewhere up in ...

  1. angularjs call method on child component
  2. angularjs call method from another controller
  3. angularjs call method from html
  4. angularjs call method on page load
  5. angularjs call method on directive
  6. angularjs call method on child directive
  7. angularjs call method
  8. angularjs call method from another component
  9. angularjs call method on parent component
  10. angularjs call method from another directive
  11. angularjs call method inside directive
  12. angularjs call method from view
  13. angularjs call method on click
  14. angularjs call method from controller
  15. angularjs call method on component
  16. angularjs call method after ng-repeat
  17. angularjs call method from child controller
  18. angularjs call method on load
  19. angularjs call method in ng-repeat
  20. angularjs call method in constructor

I'm working with a form and would like to add ng-model on dynamic input elements. I have a scope variable defined as: $scope.formData = {}; On the page there are a couple of drop-down lists that ...

I'm working with a form and would like to add ng-model on dynamic input elements. I have a scope variable defined as: $scope.formData = {}; On the page there are a couple of drop-down lists that ...

  1. angularjs dynamic model name
  2. angularjs dynamic model value
  3. angularjs dynamic ng-model inside ng-repeat
  4. angularjs dynamic ng-model
  5. angularjs dynamic ng-model name
  6. angularjs get dynamic model value
  7. angularjs ng-model dynamic variable name
  8. angularjs ng-repeat dynamic model
  9. angularjs dynamic binding ng-model
  10. angularjs ng-model dynamic value
  11. angularjs dynamic input ng-model
  12. angularjs dynamic form ng-model