Sign in to follow this  
Alpha_ProgDes

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

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[i].style.display = "none";
    }
}

Share this post


Link to post
Share on other sites

//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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this