1

So all I want to do is use a loop to set the value attribute in this code from my DOM to 0.

<input type="hidden" name="Pn_87001_qty" id="Pn_87001_qty" value="1" /> 
<input type="hidden" name="Pn_87001_qty" id="Pn_87002_qty" value="1" /> 
<input type="hidden" name="Pn_87001_qty" id="Pn_87003_qty" value="1" /> 
<input type="hidden" name="Pn_87001_qty" id="Pn_87004_qty" value="1" /> 

I have this function that creates an array with all of the id's in it.

function testing02() {
    var eles = [];
    var inputs = document.querySelectorAll('[id^="Pn_"]');
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].name.indexOf('#Pn_') == 0) {
            eles.push(inputs[i]);
        }
    }
    console.log(inputs.length); 
}

And I can change the value of individual value attributes with this code:

test01 = document.getElementById('Pn_87001_qty');
test01.setAttribute("value", "1000");

That being said, I can not figure out a way to make a loop that sets the value attribute based on the ID's from my input array. Nor can I figure out a way to make the loop above fill the input array with the entire input element so that I can write a second loop to change the values. I've tried using .getElementsByName, .getElementsByClass as well as document.querySelector.

I'm stuck and new to javascript. Any help would be greatly appreciated. I can do Jquery or Javascript

7
  • Maybe you could use a foreach loop. Commented Sep 10, 2014 at 20:55
  • Better set the value property instead of the value attribute. Commented Sep 10, 2014 at 20:55
  • You wrote .indexOf('#Pn_'). Don't you mean .indexOf('Pn_'), without the sharp sign? Commented Sep 10, 2014 at 20:57
  • Andre Yes I did, sorry. Commented Sep 10, 2014 at 21:06
  • @Oriol I tried setting the value property with .val(0) and it comes back NaN. Commented Sep 10, 2014 at 21:07

3 Answers 3

3

As you have tagged this question with jQuery, I assume a jQuery answer will suffice. To that end, you don't need a loop and can simply use:

$('[id^="Pn_"]').val(0);

Example fiddle

You could optimise this further by giving the hidden elements a common class attribute and changing the selector to use that.

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

3 Comments

I tried that command 2 or 3 times and could not get it to return anything but NaN... Until now, and I think I just realized what I was doing wrong in checking that. Thanks for your help!
One question, what are the benefits of using a common class over the id? From the reading I've been doing it seems like using the id is usually the best way to go.
The id selector is the fastest, eg. #myElement, but this can only select a single element. Your selector is the 'attribute begins with' version, which is very slow in comparison - even when selecting the id attribute. A class selector will be faster.
0

Remove the # prefix from your name.

var inputs = document.querySelectorAll('[id^="Pn_"]');
for (var i = 0; i < inputs.length; i++) {
    if (inputs[i].name.indexOf('Pn_') == 0) {
        inputs[i].value = 0;
    }
}

Demo

Comments

0

To convert the NodeList to an array, you can use:

  • for loop

    var inputs = document.querySelectorAll('[id^="Pn_"]'),
        eles = Array(inputs.length);
    for (var i = 0; i < inputs.length; ++i) {
        eles[i] = inputs[i];
    }
    
  • Array.prototype.slice

    var eles = [].slice.call(document.querySelectorAll('[id^="Pn_"]'));
    
  • Array.from (EcmaScript 6, polyfill)

    var eles = Array.from(document.querySelectorAll('[id^="Pn_"]'));
    

To set the value of all them to '0', you can use:

  • for loop

    // Note you don't have to convert `inputs` to array
    for (var i = 0; i < inputs.length; ++i) {
        inputs[i].value = '0';
    }
    
  • Array.prototype.forEach (EcmaScript 5, polyfill)

    // Note you must convert `inputs` to array
    eles.forEach(function(el) {
        el.value = '0';
    });
    
  • for...of loop (EcmaScript 6)

    // Note you don't have to convert `inputs` to array
    for(var el of inputs) {
        el.value = 0;
    }
    

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.