Filipa Lacerda
Filipa Lacerda

Reputation: 156

Javascript: how to get an element by an id?

I need to get an element from its id, something along the lines of this:

    $(this).parent().hasId('foo')

How can I accomplish this using jQuery?

Upvotes: 0

Views: 4781

Answers (5)

Val
Val

Reputation: 752

JS

document.getElementById('ID').parentNode.id

JQuery

$('#ID').parent().attr('id');

See here : JSFiddle

EDIT:

Not sure that's you asking about..

Upvotes: 2

Riz
Riz

Reputation: 10246

You can get ID of parent Div in

Javascript as:

this.parentNode.id

and jQuery:

 $(this).parent("#foo")

Upvotes: 0

Pranay Rana
Pranay Rana

Reputation: 176896

$("#idofelemnt") - make sue of id selector that is will resolve issue...

if($("#idofelemnt").length == 0)
 alert('element not exists');
else 
 alert('element exists');

or in your case

 $(this).parent("#idofelemnt").length 

Upvotes: 4

Th0rndike
Th0rndike

Reputation: 3436

something like this?

if($(this).parent().attr("id")=='foo'){
    //do your stuff;
}

Upvotes: 0

Karthik E
Karthik E

Reputation: 23

I hope you want to get element object from his id. Simply use $('#elementId') for getting that element object.

Upvotes: 0

Related Questions