0

How to check whether an element with specific attribute value is present inside a div tag.

For example,

<div id='testdiv'>
    <a dir='hello' href='#' ></a>
</div>
<input type='button' id='btn' />

Here is my Jquery.

$('#btn').click(function(){
   if(/*Here I need the condition to check whether, `a` tag with attribute `dir` with value `hello` is present inside `div` */)
   {
       alert(present);
   }
}

Please advice dear friends as I am beginner in jQuery. Thanks.

Uvais Ibrahim
  • 149
  • 4
  • 19
  • Combine the solutions to these questions: [Find all elements with a certain attribute value in jquery](http://stackoverflow.com/q/4958081) and [Is there an “exists” function for jQuery?](http://stackoverflow.com/q/31044). – Felix Kling Jun 05 '13 at 09:55

3 Answers3

0
$('#btn').click(function(){
   if($("div").find("a[dir=hello]").length == 1)
   {
       alert(present);
   }
}
Anand Shah
  • 611
  • 7
  • 14
0
$('#btn').on('click', function(){
   if ( $('#testdiv a[dir="hello"]').length ) {
       alert(present);
   }
});
adeneo
  • 312,895
  • 29
  • 395
  • 388
0
$('#btn').on('click', function(){
       if ( $('#testdiv a').attr('dir') == 'hello' ) {
           alert('1111111');
       }
        else
            alert('222');
    });

Also see this fiddle

Rinku
  • 1,078
  • 6
  • 11