0

I m new to jquery. My requirement is to pass rowid(unique id of each record of a table) in a jquery function. I can get rowid only at runtime. so how can I bind click event to the tag whose id is this rowid.

<a href="javascript:void(0)" id="`string(rowid(Gatepass))`">Upd</a>
<a href="javascript:void(0)" id="`string(rowid(Gatepass))`">Del</a>

$(what to pass here).bind('click',function(ev) {
    _operation(para1,para2); // function which is going to perfom action
    ev.preventDefault();
    return false;
});
2
  • Dont expect much help if you downvote all the answers without even commenting
    – Johan
    May 21, 2012 at 13:07
  • @Johan: I m new to this site so don't know what is this "downvote"? May 22, 2012 at 6:39

4 Answers 4

0

get the id , and acording to id do what ever you want

  $('a').on('click',function(){

    var id = $(this).attr('id');

   if(id == //what you want)
   {
      //function 1
   } 
   else
   {
      //function 2
   }     

    return false;

    });
0

If there are any similarities between the IDs, you can use one of the attribute selectors such as:

ID contains

$('[id*="something"]')

ID begins with

$('[id^="something"]')

http://api.jquery.com/category/selectors/

A better approach would be to place all of the dynamically named anchors into a container, and then select on that:

<div id="container">
    <a ...></a>
    <a ...></a>
</div>

Then you would select all the child anchors:

$('#container > a').click(...);
0
0

It's hard to find a good selector from so few HTML code. Use a class on your markup if possible:

<a class="roww" href="javascript:void(0)" id="`string(rowid(Gatepass))`">Upd</a>
<a class="roww" href="javascript:void(0)" id="`string(rowid(Gatepass))`">Del</a>

then you can use $('.roww') to query your nodes.

Here's what you can do to get the id from the event handler:

function( ev ) {
   //wrap the element with jQ:
   var jel = $(this);
   //Then access attributes with .attr() getter:
   var id = jel.attr('id');

   ... //do whatever you want now.
   ... // there's a quicker alternative to get the id without jQuery, simply: 
   ... // var id = this.id
}
0

if the id comes dynamically from the server, put inside the function the same id + hash for id selctor

$(what to pass here) => $("'#" + string(rowid(Gatepass)) + "'")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.