Question

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?

Was it helpful?

Solution

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].

OTHER TIPS

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top