Reputation: 883
I need to pass the parameter to jquery function from the link button.I need to pass val1 ,val 2 values to getdata function.
var val="Need to pass this first string to another jquery Function";
var val2="Need to pass this second string to another jquery Function";
<a id='myLink' href='#' onclick='getData()' >Link Text</a>
function getdata()
{
//I need to here
}
Upvotes: 0
Views: 1791
Reputation: 24638
If the values are defined as you have, it does not make sense to pass them to the function; they are available to the function already.
We use arguments to pass data that's specific to the element that was interacted with via the event. One advantage of using jQuery is that inside the function 'this' refers to the element that was, clicked for example. This you can access all it's properties and attributes.
$('#myLink').on('click', getdata);
function getdata() {
var lnText = $(this).text(); // for example
//
}
Inline JS is not recommended:
<a id='myLink' href='#'>Link Text</a>
Upvotes: 0
Reputation: 15393
Pass Parameter in event handler like this
var val="Need to pass this first string to another jquery Function";
var val2="Need to pass this second string to another jquery Function";
$('#myLink').on("click" , {arg1:val , arg2:val2}, getData);
function getData(event){
event.preventDefault();
alert(event.data.arg1); // Need to pass this first string to another jquery Function
alert(event.data.arg2); // Need to pass this second string to another jquery Function
}
Upvotes: 0
Reputation: 1453
You don't need to pass it through links, put those values in some hidden field like bellow:
<input type="hidden" id="val" value="Need to pass this first string to another jquery Function">
now get it in jquery function as bellow;
var value1 = $('#val').val();
Upvotes: 1
Reputation: 333
This works quite well:
HTML
<a id='myLink' href='#' onclick="getData(this)">Link Text</a>
Javascript
function getData(element){
alert(element.innerHTML);
}
Upvotes: 0
Reputation: 28823
Did you try something like this?
$('#myLink').click(function () {
getdata(val, val2);
});
This will be placed in js file. Not html file. But this works fine.
Hope this helps.
Upvotes: 0