0

I am creating a table that changes size based on a javascript array. I am modeling reservations and when the page loads it loops through an array of reservation objects and creates a table to represent them. I have a checkbox created as the first element of each row as such:

checkboxCell.innerHTML = "<input type='checkbox'>";

I want to be able to know which row(or checkbox) I am clicking when they are pressed. Since these are being creating in one line, they would all have the same ID. So I either need a way to know where I am in the table or a way to create a different ID for each of these elements.

1
  • 3
    Do yourself a big favor and start using a library like Knockout.js. It will make your life so much easier. Commented Sep 28, 2015 at 22:44

1 Answer 1

2

You can generate incremental ID's for your new elements within the loop which is creating the new HTML elements, e.g. assuming you're appending inputs to an element with an ID of parent-element:

    function makeElements(){
      var parent = document.getElementById('parent-element');
      for (var i = 1; i < 11; i++) {
        var newElement = document.createElement('input');
            newElement.id = 'input-number-' + i;
            newElement.type = "checkbox";
        parent.appendChild(newElement);
      }
    }

As commented by connexo however you are probably better off using an existing library to take care of this sort of thing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.