SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Member cssbleach's Avatar
    Join Date
    May 2010
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    SitePoint Evangelist priti's Avatar
    Join Date
    Aug 2006
    Location
    India
    Posts
    488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In addition to priti's solution, if you are looping through html elements you may use:

    Code:
    element.attr('id')     //jQuery
    element.id             //JavaScript
    where 'element' is an HTML element.

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,081
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Note that the question is:
    Quote Originally Posted by cssbleach View Post
    How can I check if a html tag has an "id" attribute or not, using Javascript and also using jQuery?
    and that the question is not how do I check if an element with a certain id exists in the DOM?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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!

    Code:
    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 + "\n";
    }
    alert ("These links have IDs:\n\n" + str);
    Ian Anderson
    www.siteguru.co.uk

  6. #6
    SitePoint Member cssbleach's Avatar
    Join Date
    May 2010
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code:
    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:

    Code:
    <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?

  7. #7
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,081
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Try != null instead of != "undefined"
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  8. #8
    SitePoint Member cssbleach's Avatar
    Join Date
    May 2010
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  9. #9
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try using an empty string ("").


  10. #10
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  11. #11
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I did say untested.
    Ian Anderson
    www.siteguru.co.uk

  12. #12
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,826
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    if(''!==element.id) is the safest way to test it (any typo should result in a syntax error).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  13. #13
    SitePoint Member cssbleach's Avatar
    Join Date
    May 2010
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •