1

I want to send multiple data from on click function to jquery function like this code.

When I click on my button I get [object Object]undefined] in alert box

Code :

$(function() {
  $("#button").click(function myf(data1, data2) {
    alert(data1 + data2);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" id="button" onclick="myf('hello','word');">click</a>

How can I do it?

4
  • 1
    You're binding two click event listeners! One using .click() and the other using onclick attribute! Commented Apr 5, 2017 at 13:56
  • 1
    You're registering 2 event listeners, and there's no point in passing the data from the HTML if you're binding the events on the element's id. The id must be unique, so you might as well code the data into the function. Commented Apr 5, 2017 at 13:57
  • What's wrong with the javascript listener you've added? Why do you need a jQuery one also? Commented Apr 5, 2017 at 13:57
  • I have not received any valid result from any answer yet Commented Apr 5, 2017 at 14:09

4 Answers 4

3

There's absolutely no point in passing the data from HTML.

You're binding that function to the button's id. That id must be unique, so there's only ever that single combination of values.

Just use this:

$(function() {
  $("#button").click(function() {
    alert('hello' + 'word');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" id="button">click</a>

Or, if you want to send the data from the HTML, you're going to have to do something like this:

function mf (data1, data2){
    alert(data1 + data2);
}
<a href="#" id="button" onclick="mf('hello','word');">click</a>

Or, if you want to use jQuery, you're going to have to do something convoluted like this:

$("#button").click(function() {
    var data = $(this).data('params').split(',');
    alert(data[0] + data[1]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" id="button" data-params='hello,word'>click</a>

Sign up to request clarification or add additional context in comments.

7 Comments

my value not fixed i have many button each of ther have value
@mody - your building buttons dynamically?
i want with jquery
@mody: why do you want to use jQuery?
Fix the typo mistake on second snippet ... name of function
|
2

You are using 2 event that will both try to listen to the onclick event you can remove one of them like so :

<script>
$(function() {
  $("#button").click(function() {
  alert('hello' + 'word');
   });
});
</script>

An id is always unique so you can put your value directly into the function.

if you really want to use jquery you can use this:

$("#button").click(function() {
var data = $(this).data('params').split('|');
alert(data[0] + data[1]);
});

And this for the html :

<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> 
</script>
<a href="#" id="button" data-params='hello|world'>click</a>

7 Comments

helllo word not fixed value
@mody can you explain what those value are exaclty then and is jquery really neccesary because javascript could be a simple solution
i have tow value inside function from button (show in my question ).. when i clcik on button i want the value send to jquery function .. i have another code to useing those tow value but i using alert only for example
@mody: "2" is spelled "two", not "tow"
@mody this should work normally for what you need to do
|
1

Following code should do the trick:

function myf(data1, data2) {
  alert(data1 + data2);
  return false;
}
<a id="button" href="#" onclick="myf('hello', 'word');">click me</a>

return false; is to prevent default action.

9 Comments

No, now you're passing the result of myf('hello','word') to .on. That's literally the same as $("#button").on("click", false);
no i want send from onclick to jquery and do not fixed value
Edited my answer, still any problems with it?
@mody Now the values are bound purely on the id the click was bound on. If you now add other clickable items you can change the function call in their click handlers.
@Peter-Paul: That doesn't fix the problem I told you. The problem is in the .on("click", myf('hello','world')). That doesn't work.
|
0

As said you have already a listener, I don't see the point of adding an other one.

<button onclick="myf('hello','word')">click</button>

<script>
function myf(data1,data2){
  alert(data1+data2);
}
</script>

https://jsfiddle.net/wnd6ovkf/

And the jquery version:

<button data1= "value" data2= "value2" class="btn">click</button>
<button data1= "ohtervalue" data2= "othervalue2" class="btn">click</button>

<script>
$(".btn").click(function() {
  alert(this.getAttribute("data1") + " " + this.getAttribute("data2"));
});
</script>

https://jsfiddle.net/wnd6ovkf/6/

6 Comments

i know this sir .. i want with jquery
@mody added jquery as asked.
hello word not fixed value it changing .. i want send data from button function to jqure and jquery display it
@mody I think this is what you want.
Develop your latest own code so that you send two values to the jammer through only one button. In order to agree to your answer
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.