2

I have 2 input tags:

<input type="text" class="mainText" id="input">
<input type="text" class="mainText">

I need to set id to the input tag that don't have id, may be with jQuery like:

if(input tag has no id){
    $(".mainText").attr("id", "someInput");
}

How could I find this non id input?

Lewis
  • 14,132
  • 12
  • 66
  • 87

2 Answers2

4

Use the magic of jQuery selectors:

$('.mainText:not([id])').prop('id', 'someInput');

It will select all elements with .mainText class that does :not have attribute [id].

VisioN
  • 143,310
  • 32
  • 282
  • 281
0

.has(your ID) will return false if no ID been found.

see document here: http://api.jquery.com/has/

nightire
  • 1,371
  • 2
  • 11
  • 21