Answer the question
In order to leave comments, you need to log in
Passing parameters to a function when clicking on a link (JQuery)?
In pure JavaScript, it looks something like this:
<sсript><br/>
function toFunction(m1, m2) {<br/>
alert(m1 + ' - ' + m2);<br/>
}<br/>
</sсript><br/>
<а href="javascript:toFunction(10,'text')" >Ссылка</а>
<sсript><br/>
$(document).ready(function() {<br/>
$(".urlparam").click(function () {<br/>
<br/>
alert(m1 + ' - ' + m2);<br/>
<br/>
});<br/>
});<br/>
Answer the question
In order to leave comments, you need to log in
Perhaps it makes sense to use the data-* attribute from html5, instead of crutches on class / rel / etc., which are not intended for this?
+ use of javascript pseudo-protocol: evil ;).
With what fright my code cannot be used for several links, I did not understand.
Once:
function toFunction(){};
$(".urlparam1").click(function () { toFunction(10,'text'); });
$(".urlparam2").click(function () { toFunction(9,'text1'); });
function toFunction(){};
$(".urlparam1").click({num: 10, text: 'text'}, toFunction);
$(".urlparam2").click({num: 9, text: 'text1'}, toFunction);
<а href="#" class="urlparam" data-num="10" data-text="text"></a>
<а href="#" class="urlparam" data-num="9" data-text="text1"></a>
$(".urlparam").click(function () {
var num = $(this).data('num');
var text = $(this).data('text');
});
First, you can use eventData (see api.jquery.com/click/ ).
Second, you can still write:
$(".urlparam").click(function () {
toFunction(10,'text');
});
Interest Ask. If I need one parameter, then I use the href="#" attribute. If there are several parameters:
PS And I also use rel instead of class.
<а href="#" class="urlparam">
Ссылка
<input type="hidden" name="param1" value="1">
<input type="hidden" name="param2" value="2">
</а>
$(".urlparam").click(function () {
var param1 = $(self).find('param1').val();
});
$('.urlparam').each(function(index) {
$(this).click({num: (index+1), text: $(this).value()},toFunction);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question