I am facing a problem in which I need to pass either one or multiple parameter to a javascript
function. For example
<a href="#" OnClick="Delete(1,'q');" > X </a>
<a href="#" OnClick="Delete(2,'u');" > X </a>
But I am trying to avoid this onclick
attribute from the html end. So I have used this way
<a href="javascript://" title="Delete" id="actRemove" data-action="/List/Delete/1" ><span class="glyphicon glyphicon-remove"></span>Delete1</a>
<a href="javascript://" title="Delete" id="actRemove" data-action="/List/Delete/2" ><span class="glyphicon glyphicon-remove"></span>Delete2</a>
<a href="javascript://" title="Delete" id="actRemove" data-action="/List/Delete/2" ><span class="glyphicon glyphicon-remove"></span>Delete3</a>
And written the jQuery
function like this to capture my click function from the link
$("#actRemove").each(function() {
$(this).on("click", function () {
alert($(this).data("action"));
});
});
But alas!! I am undone. This link works only for the first anchor Delete1
none of the anchors are working. Here is my jsFiddle link. I have gone through these answers Q1, Q2, Q3, Q4, Q5, Q6. Each of those question have the solution in the first way using onclick
and passed the parameter to the function.I am thinking differently. Moreover my another question is, is there any way to pass parameter to a jQuery
function
without writing onclick
in the html
attribute? I know that jQuery function is capable to receive the parameter, but how can I send it to the function from the html end?