How can I pass a value in a jQuery function without using the onclick attribute?

I am facing a problem where I need to pass one or more parameters to a function javascript

. for example

<a href="#" OnClick="Delete(1,'q');" > X </a>
<a href="#" OnClick="Delete(2,'u');" > X </a>

      

But I am trying to escape this attribute onclick

from the end of the html. So I used this path

<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 wrote a function jQuery

like this to grab my link click function

$("#actRemove").each(function() {
   $(this).on("click", function () {
      alert($(this).data("action"));
   });
});

      

But alas !! I'm sad. This link only works for the first anchor, Delete1

none of the anchors works. Here is a jsFiddle link . I have reviewed these answers Q1 , Q2 , Q3 , Q4 , Q5 , Q6 . Each of these questions has a solution in the first way using onclick

and passed a function parameter. I think differently. Also, my other question is, is there a way to pass a parameter in jQuery

function

without writing onclick

in the attribute html

?
I know the jQuery function is capable of taking a parameter, but how can I send it to the function from the end of the html?

+3


source to share


3 answers


id

Use a selector instead class

: -

HTML: -

<a  href="javascript:viod(0);" title="Delete" class="actRemove" data-action="/List/Delete/1" ><span class="glyphicon glyphicon-remove"></span>Delete1</a>
<a  href="javascript:viod(0);" title="Delete" class="actRemove" data-action="/List/Delete/2" ><span class="glyphicon glyphicon-remove"></span>Delete2</a>
<a  href="javascript:viod(0);" title="Delete" class="actRemove" data-action="/List/Delete/2" ><span class="glyphicon glyphicon-remove"></span>Delete3</a>

      

JQuery: -

$(".actRemove").on("click", function () {
  alert($(this).data("action"));
});

      



Working demo: -

$(".actRemove").on("click", function () {
  alert($(this).data("action"));
});
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a  href="javascript:viod(0);" title="Delete" class="actRemove" data-action="/List/Delete/1" ><span class="glyphicon glyphicon-remove"></span>Delete1</a>
<a  href="javascript:viod(0);" title="Delete" class="actRemove" data-action="/List/Delete/2" ><span class="glyphicon glyphicon-remove"></span>Delete2</a>
<a  href="javascript:viod(0);" title="Delete" class="actRemove" data-action="/List/Delete/2" ><span class="glyphicon glyphicon-remove"></span>Delete3</a>
      

Run codeHide result


Note: -

id

used like unique-selector

in jQuery and class

used likegroup-selector

+5


source


You can use this way



<a  href="javascript://" title="Delete" id="actRemove_1" data-action="/List/Delete/1" ><span class="glyphicon glyphicon-remove"></span>Delete1</a>
<a  href="javascript://" title="Delete" id="actRemove_2" data-action="/List/Delete/2" ><span class="glyphicon glyphicon-remove"></span>Delete2</a>
<a  href="javascript://" title="Delete" id="actRemove_3" data-action="/List/Delete/2" ><span class="glyphicon glyphicon-remove"></span>Delete3</a>



$("a[id^='actRemove_']").on("click", function () {
     alert($(this).data("action"));
})

      

+2


source


You can get the value of the HTML object directly in javascript without passing it with onclick using:

var object_id = document.getElementById('object_id').value;

      

+1


source







All Articles