tags:

views:

19

answers:

1

I have a div which could potentially have a hyperlink with an id of reply. How can I check if that a[id=reply] exists? I thought it might be something like this but it alerts the message even if that hyperlink does not exist. Thanks in advance for any help

    if($('div[chunk_id='+reply_chunk_id+']').children('a[id=reply]')){              
alert('test');
    }
+3  A: 

Check the .length of the selector to see how many elements it matched, in this case:

if($("#reply").length) {
  //child exists
}

However, it sounds like you have multiple elements with id="reply", which is invalid. Instead use class="reply" and your selector will look like this:

if($("div[chunk_id='"+reply_chunk_id+"'] > a.reply").length){  
  //child exists
}
Nick Craver
+1, won by 26 seconds :o Edited for missing quote.
Matchu
@Scarface - You should use a class instead...an ID should appear only once per page.
Nick Craver
thanks a lot Nick as usual, appreciate it
Scarface
@Scarface - welcome :)
Nick Craver
Use `"+reply_chunk_id+"` instead of `'+reply_chunk_id+'`, otherwise it sees it as a value, not a variable.
Alec
@Alec - yep already noticed ;)
Nick Craver