0

I am trying to find all the elements of engagement_app which have ids and which do not have ids. What is wrong with the below code? It cannot find the ones with/without ids

jQuery('.engagement_data').each(function() {

   var engagement_app = $(this).find(".engagement_app").val();
   //if ($(this).find(".engagement_app").attr('id'))
   if ($(this).find(".engagement_app").is('[id]'))
   {
      console.log("in if1")
      console.log($(this).find(".engagement_app").attr('id'));
   }

   if ($(this).find(".engagement_app").not('[id]'))
   {
        console.log("id not found")
   }
});
Gone Coding
  • 90,552
  • 24
  • 176
  • 195
Hulk
  • 30,904
  • 60
  • 142
  • 212
  • You should have a look at the documentation to learn what `.not` actually does: http://api.jquery.com/not/. Also see: http://stackoverflow.com/questions/31044/is-there-an-exists-function-for-jquery?rq=1. – Felix Kling Jul 30 '13 at 09:10

4 Answers4

2

Try this -

use ! instead of not

if (! $(this).find(".engagement_app").is('[id]')){
    console.log("id not found")
}else{
    console.log("in if1")
    console.log($(this).find(".engagement_app").attr('id'));
}
Mohammad Adil
  • 44,013
  • 17
  • 87
  • 109
0

If you want "all the elements of engagement_app" (chilldren?) with and without id you should use something like that.

var els_with_id = $(this).find(".engagement_app *[id]") 

and

var els_without_ids = $(this).find(".engagement_app *").not("[id]")
Luca Rainone
  • 15,578
  • 2
  • 37
  • 51
0

You can use

$('.engagement_app [id]')

To find all elements with ID, and

$('.engagement_app :not([id])')

To find all elements without ID.

Here's a fiddle that demonstrates how this could work:

http://jsfiddle.net/qj3V7/

EmirCalabuch
  • 4,642
  • 1
  • 23
  • 19
0
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function(){

$(".engagement_app").each(function(){
var id = $(this).attr("id");

if(id==undefined)
{
  console.log("id not found")
}
else
{
  console.log("in if1");
  console.log($(this).attr('id'));
}
});


});
</script>
</head>
<body>
<div class="engagement_data">
<div class="engagement_app" id="1">1</div>
    <div class="engagement_app">2</div>
    <div class="engagement_app" id="3">3</div>
    <div class="engagement_app">4</div>
</div>

</body>
</html>