1

I need to create an array of the id's from each <input type="checkbox"> in a selected div.

HTML

<div id="ADC-Designer-CAD">
<table>
<tr>
  <td>Teamcenter Completed:</td>
  <td><input type="checkbox" id="tc" onChange="mtcb()" /></td>
  <td><input type="button" id="tc" class="btn btn-default"onClick="ScrollToKP();" /></td>
</tr>
<tr>
  <td>NX Manager Completed:</td>
  <td><input type="checkbox" id="nxm" onChange="mtcb()" /></td>
  <td><input type="button" id="nxm" class="btn btn-default"onClick="ScrollToKP();" /></td>
</tr>
</table>
</div>

There are multiple div's like this one with different id's to them. My code selects the div first. I can get it to filter the correct div. I can't get it to create the array of the check box id's inside that div.

JavaScript

$.get('content/page.php', function(data) {
    data1 = $(data).filter('#' + actSite + '-' + actUT + '-CAD');
    data2 = $(data1).filter(`*Not sure what to put here*`);
});

data1 works as it should and gets from <table>...</table> I need something to end looking like

data1 = ["tc", "nxm", ...]

If I were to create an array for each item I need an array for, it would take weeks. I need to be able to change the data in data1 over and over again.

EDIT / SOLUTION - 4/21/2015

I have changed all my ID's for the checkboxes as many have commented. Here is my WORKING code:

var NewArray = [];
$(':checkbox').each(function(index, element) {
    NewArray.push(this.id);
});
//results NewArray = [ADC-Designer-CAD-tc,ADC-Designer-CAD-nxm,....]    
6
  • 1
    Your HTML IDs must be unique. Commented Apr 15, 2015 at 21:09
  • Yes they are. Only way to keep items I need seperated. Commented Apr 15, 2015 at 21:10
  • No they're not. You duplicate tc and nxm. Commented Apr 15, 2015 at 21:11
  • They are not, you have a button with ID tc and a checkbox with the same ID Commented Apr 15, 2015 at 21:11
  • There is no way to differentiate between a button and a checkbox? I have them the same because I use them to fill variables in another function. Commented Apr 15, 2015 at 21:12

2 Answers 2

1

Try javascript like this:

var result = new Array();
$("#ADC-Designer-CAD input[type=checkbox]").each(function(index, element) {
result.push($(element).attr("id"));
});
Sign up to request clarification or add additional context in comments.

1 Comment

I agree with this solution. This is the approach I use all the time.
1

Are you sure you want to use JQuery ?

I think this might do the job :

function getIDs() {
    var inputs = document.getElementsByTagName("input")
    var i;
    var checkboxes = [];
    for (i = 0 ; i < inputs.length ; i++) {
    if (inputs[i].getAttribute("type") == "checkbox")
        checkboxes.push(inputs[i].id);
    }
    return checkboxes;
}

Basicaly, I search in all input elements which ones has the checkbox type. Then, I push the ID in an array.

And I highly recommend to have only ONE element with an ID. Having two elements with the same ID is bad.

That's why we have getElementById() with no 's' and getElementsByClassName() with an 's'

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.