views:

28

answers:

2
      $(".mylink").live('click', function(){
    var linkid = $(this).attr('id');
    if($("#TBL").find('tbody > '+ #linkid ).css("display")=="inline"){
        // Do Somerthing 
     }
     });

I have to put the Dynamic variable "linkid" in to the above if condition iam looking to have the interpreted value .Iam getting "invalid character error.how do i do this

+1  A: 

Should be as simple as:

.find("tbody > #" + linkid);

You can put the # sign directly into Javascript. Because #linkid is not valid Javascript.

To be extra careful, you might want to use a regular expression to escape CSS3 characters that are in the ID. See here for more information on what characters need to be escaped.

UPDATE

Nick's answer is correct and works for your situation. I didn't notice that you were trying to use the id of the same element that you clicked on. this refers to the element that generated the click and so you shouldn't have to grab it again via jQuery.

In general, what I wrote above will work if you want to use a variable that contains an id, in a jQuery selector string.

Vivin Paliath
+1  A: 

You can just do this:

$(".mylink").live('click', function() {
  if ($(this).css("display")=="inline") {
    // Do Somerthing 
  }
});

Since IDs should be unique, you can just use this, you should already have the only element with that ID. If you have multiple elements with the same ID that's another issue that needs addressing, perhaps you should be using href="#id" for example, and $(this.hash) in the handler.

Nick Craver