stack
stack

Reputation: 10218

How to select element which doesn't has Id attribute?

I have two textarea and one of them has id attribute and other one doesn't has. Something like this:

<textarea> </textarea>
<textarea id = 'idname'> </textarea>

Now I need to select first textarea, How can I do that?

Upvotes: 3

Views: 215

Answers (4)

Dzianis Yafimau
Dzianis Yafimau

Reputation: 2016

Try to use the following in your case:

$('textarea:not(#idname)')[0]

Selector depends on exact requirements and mark-up

Upvotes: 0

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

JQuery solution :

Using :not() Selector :

$('textarea:not(#idname)')

Hope this helps.


 $('textarea:not(#idname)').text('selected');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea> </textarea>
<textarea id='idname'> </textarea>

Upvotes: 0

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93551

As you also don't mind a jQuery answer:

$('textarea:not([id])');

or

$('textarea').not('[id]');

Update:

For a specific name:

$('textarea[name="somename"]');

Upvotes: 3

Josh Crozier
Josh Crozier

Reputation: 240858

You could combine the :not() pseudo-class and the [id] attribute selector in order to negate elements with an id attribute:

textarea:not([id]) {}
document.querySelectorAll('textarea:not([id])');
$('textarea:not([id])');

Basic Example:

textarea:not([id]) {
  width: 100%;
}
<textarea></textarea>
<textarea id='idname'></textarea>

Upvotes: 5

Related Questions