How to check if a tag has an "id"

Hi

How can I check if a html tag has an “id” attribute or not, using Javascript and also using jQuery? (I want both solutions, please).

Many thanks

jquery - $(‘#idname’).val()
js -document.getElementById(‘idname’)

Both will return undefined if idname not found. Kindly check for “js” part. I am sure for jquery.

This can also be used to identify whether you have defined id for element or not.

In addition to priti’s solution, if you are looping through html elements you may use:

element.attr('id')     //jQuery
element.id             //JavaScript

where ‘element’ is an HTML element.

Note that the question is:

and that the question is not how do I check if an element with a certain id exists in the DOM?

Do you have specific tags in question? (e.g. <A>, <P>, <DIV>)

document.getElementsByTagName(‘a’) should get you an “array” of the <A> tags. Then you could check for the .id attribute.

UNTESTED!

var links = document.getElementsByTagName('a');
var len = links.length;
var str = "";
for (var i=0; i<len; i++) {
  if (links[i].id != 'undefined') str += links[i].id + "\
";
}
alert ("These links have IDs:\
\
" + str);

My question is exactly as how ScallioXTX has mentioned and Siteguru also got it.

I have tried Siteguru’s solution but it’s not working properly. (thanks, btw.)

I have modified it to this:

var cells = document.getElementsByTagName("td");
var len = cells.length;
var str = "";
for (var i=0; i<len; i++) {
	if (cells[i].id != "undefined"){
		alert("Id in here");
	}else{
		alert ("No id here!");
	}
}

And this is the HTML:

<table id="tbl">
	<tr>
		<td><a href="#">a</a></td>
		<td><a href="#">b</a></td>
		<td><a href="#">c</a></td>
		<td><a href="#">d</a></td>
	</tr>
</table>

Clearly there are no ‘id’ attributes in those ‘td’ cells, as you can see. But the javascript still hold the condition as ‘true’ even though there are no ‘id’ at all.

I don’t understand, can you help, please?

Try != null instead of != “undefined”

I’ve just tried ‘null’ and it’s still doesn’t work.

I think something in the code is not right but I don’t see any problem with it though.

Try using an empty string (“”).

:eye:

if(x==undefined) and if(x==null) are only true if x is undefined or null, not if it is false, 0,NaN or the empty string.

if(x!=‘undefined’) is true if the value is not the string ‘undefined’

an element id is never undefined or null- most html elements have an id property,
though until it has been assigned, in the html or a script, its value is ‘’, the empty string.

if(element.getAttribute(id)) or if(element.id) or if(element.id !==‘’)
all return false if the id has not been set.

The first 2 return the id, the third returns true, if there is an id.

I did say untested. :wink:

if(‘’!==element.id) is the safest way to test it (any typo should result in a syntax error).

Hi guys

@Mrhoo, I used your advice and I managed to get it work in the end. It was as easy as “if(element.id)”, so thank you.

And also many thanks to all the guys in here, for your help.