javascript - Pass arguments from a button click to Jquery event handler - Stack Overflow

I am trying to pass the argument to the JQuery event handler when a button is being clicked. In plain J

I am trying to pass the argument to the JQuery event handler when a button is being clicked. In plain JavaScript i was using onclick="function(argument)" Now i am transferring all of my inline click events to external call like one with Jquery i.e

 $(".selector").click(function(){
//some code
});

However, in this case i am confused how i can pass the arguments from the HTML tag and how should i receive the arguments in the Jquery event handler. Please help.

I am trying to pass the argument to the JQuery event handler when a button is being clicked. In plain JavaScript i was using onclick="function(argument)" Now i am transferring all of my inline click events to external call like one with Jquery i.e

 $(".selector").click(function(){
//some code
});

However, in this case i am confused how i can pass the arguments from the HTML tag and how should i receive the arguments in the Jquery event handler. Please help.

Share edited Jan 18, 2018 at 3:24 Victor asked Jan 18, 2018 at 3:18 Victor Victor 671 gold badge2 silver badges11 bronze badges 2
  • can you please share the html and show which argument you want to pass – brk Commented Jan 18, 2018 at 3:25
  • Can you add a plete example of your Javascript function? It could help answerers to convert it to a Jquery function – Raul Sauco Commented Jan 18, 2018 at 3:31
Add a ment  | 

3 Answers 3

Reset to default 2

An optional object of data passed to an event method when the current executing handler is bound.

In your case it will be something like this -

$(".selector").on("click", function(event){
    //you can access the parameter value as follows
    console.log(event.target.value);
});

Refer the official documentation here Pass arguments from a button click to Jquery event handler

The .click() function receives an EventData object that gets passed as the first argument to the handler, you can use that object inside the handler.

For example:

$('.selector').click(function(data) {
  $(data).addClass('newClass');
});

If you are trying to pass data from the HTML the cleanest way might be to store that data into the data attribute of your tag.

The HTML

<div id="mydiv" data-arg="Hello world!">Click me</div>

The JS

$('#mydiv').click(function(data) {
  var arg = $(data).attr('data-arg');
  console.log(arg);
});

For what you are trying to achieve

The HTML

<button id="dark" data-stroke-width="0.5" 
  class="txtcolor uk-button" type="button">1px</button>

The JS

$('button#dark').click(function(data) {
  selectStroke($(data).attr('data-stroke-width'));
});    

Check the documentation for click

A sample:

// say your selector and click handler looks something like this...
$("some selector").click({param1: "Hello", param2: "World"}, cool_function);

// in your function, just grab the event object and go crazy...
function cool_function(event){
    alert(event.data.param1);
    alert(event.data.param2);
}

var param_obj = {data : {param1: "Hello", param2: "World"}};
cool_function(param_obj);


$("dark").click({param1: 0.5}, selectStroke);

or

var i = 0.5;
$("dark").click({param1: i}, selectStroke);

or if you do not want change functionality of the handler

var i = 0.5;
$("dark").click({param1: i}, function() {
    selectStroke(event.data.param1);
);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744914498a4600752.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信