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")
   }
});
iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
Hulk
  • 32,860
  • 62
  • 144
  • 215
  • 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'));
}
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
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
  • 16,138
  • 2
  • 38
  • 52
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,756
  • 1
  • 25
  • 20
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>