Jump to content
  • Advertisement
Sign in to follow this  
Alpha_ProgDes

Getting an index from an array of tables by id (javascript)

This topic is 3101 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have an array of tables and I would like to get the index of the table that has a particular id. Without looping. Is that possible?
function testTableIdea() {

    var formTables = document.getElementsByTagName("table");
    var idIndex = formTables["ThisOne"].index;  //<--------something like this
    
    for (var i = idIndex; i < formTables.length; i++) {
        formTables.style.display = "none";
    }
}

Share this post


Link to post
Share on other sites
Advertisement

//Given
<table id="ThisOne">...

...

var table = document.getElementById("ThisOne");

There is obviously still the problem with potentially multiple identical ids, or id not found.


Usually, jQuery or similar are used for this purpose. With jQuery it would be something like:
$("#ThisOne").index

Share this post


Link to post
Share on other sites
When inserting the table into the array, you could set a .index property of the table element to the current index. Later you can retrieve it:

var index = document.getElementById("ThisOne").index;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!