1

I'm trying to get the input type value's from input fields that have been generated by a webshop framework. I can't change the html. When I added fields it creates this:

<input id="filter_15" type="checkbox" name="filter[]" value="15">
<input id="filter_16" type="checkbox" name="filter[]" value="16">
<input id="filter_17" type="checkbox" name="filter[]" value="17">

I found some code that gets the value based on the input id, except that the id's filter_... can change, and are different for every shop.

any sugestions? here's what i've gathered so far.

<script type="text/javascript">
var filter;
function onload() {
    filter = document.getElementById('filter');
}
function show() {
    alert(filter.value);
}

1
  • maybe document.querySelectorAll('input[type="checkbox"]'); is usefull here. It'll return your a node list containing all the inputs of the type checkbox. Then you can loop over them and get the values. Commented May 11, 2016 at 9:09

4 Answers 4

7

You were on the right track with .getElementById, but you want instead, .getElementsByName.

var els = document.getElementsByName("filter[]");

for (var i = 0; i < els.length; i++)
  alert(els[i].value);
<input id="filter_15" type="checkbox" name="filter[]" value="15">
<input id="filter_16" type="checkbox" name="filter[]" value="16">
<input id="filter_17" type="checkbox" name="filter[]" value="17">

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

Comments

3

By using document.querySelectorAll, in which you can use any CSS selector with, including an "attribute starts with" selector like input[id^="filter_"] must get your job done.

Here is a demo

var inputs = document.querySelectorAll('input[id^="filter_"]');
debugger;
for (i = 0; i < inputs.length; i++) {
    alert(inputs[i].value)
}
<input id="filter_15" type="checkbox" name="filter[]" value="15">
<input id="filter_16" type="checkbox" name="filter[]" value="16">
<input id="filter_17" type="checkbox" name="filter[]" value="17">

Comments

1

Heres the Jquery solution

$('input [id^="filter_"]').each(function(){
   alert( $(this).val() );
});

4 Comments

OP states that the id is not always "filter_..."
@VelimirTchatchevsky the OP states that only the part after the underscore might change like filter_xxx
@VelimirTchatchevsky, OP is saying for id suffix(filter_....) can be changed, but it;s also not right answer, OP is looking solution in javascript
I know the solution was requested in javascript but it's always nice to know all potential solutions I think, as you will likely come across both solutions for solving this problem in other systems.
1

You can get inputs by tag name, then iterate over it.

    var inputs, index, len;

    inputs = document.getElementsByTagName('input');
    len = inputs.length;

    for (index = 0; index < len; ++index) {
        alert(inputs[index].value)
    }

4 Comments

I am not the downvoter but your solution selects more elements than necessary. Using getElementsByName() will lower the number of elements to loop through and therefor will be quicker.
it's not right solution, the html page might be many input fields, getElementsByTagName() method can't target specific elements.
The title states: "for each input type get value with javascript" That makes the other approach wrong.
That is right, I will upvote if you will make one change :). This is something that is 'wrong' in many solutions that use the for-loop. Don't recalculate the length of the array with every loop. Calculate the array's length before the loop, put it in a variable and use that in the loop condition.

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.