0

im trying to gather an amount from a input box and then tick a certain amount of boxes which is an array.

this is what i got so far and it doesnt work :S

function checkAmount(ting)  {  
var boxes = document.getElementsByTagName("input");  
for (var i = 0; i < ting.value; i++)   {  
    if (boxes[i].name == "vote[]")   {  
        boxes[i].checked = true;  
    }  
}  
}

And im calling it with this:

uncheckAll(); 
checkAmount(document.getElementsByName(\'ammount\'));
2
  • is all your vote-inputs named "vote[]"? How do you know what is voted? I think it would be a good idea to look at jQuery.. Commented Jan 7, 2011 at 22:41
  • in the loop, it's not ting.value but ting.length Commented Jan 7, 2011 at 22:42

5 Answers 5

2

getElementsByName returns an array and your function is expecting a single element, you need to access the first element like this:

checkAmount(document.getElementsByName(\'ammount\')[0]); 

Change the ting.value to boxes.length in the for loop:

function checkAmount(ting)  {  
var boxes = document.getElementsByTagName("input");  
for (var i = 0; i < boxes.length; i++)   {  
        boxes[i].checked = (boxes[i].name == "vote[]") ;  
  }  
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

boxes.item(i).checked = checked

sorry not sure how to insert javascript into the post

Comments

0

are you posting your real code? If so, you realize that you are spelling 'amount' wrong in your function call?

From your statement "gather an amount from an input box" I think you expect ting to be a number entered into a text box and so you are calling a function that expects a number with a value that should resolve to an array of form element objects?

var m = document.getElementsByName("amount");

m will be an array!

in other words, it might work better if you made sure you were passing a number when you call checkAmount:

checkAmount(documents.forms[0].amount.value)

or:

checkAmount(documents.getElementsByName("amount")[0].value)

It would help a lot if you post the actual HTML and actual javascript code.

Comments

0

There are several problems with the code you posted, as indicated by the previous answers and replies. Try something like this:

function checkAmount(ting)  {  
    var boxes = document.getElementsByTagName("input");  
    for (var i = 0; i < ting.value && i < boxes.length; i++)   {  
        if (boxes[i].name == "vote[]")   {  
            boxes[i].checked = true;  
        }  
    }  
}

Called like this:

uncheckAll(); 
checkAmount(document.getElementsByName(\'ammount\')[0]);

Now you are safe from array out of bounds errors no matter the value typed in the input field and the call to checkAmount should work.

1 Comment

hi the call is solved now because even when i alert it it gives me the value but still the checkboxes arnt checked
0

Try using

checked=checked

instead of

checked=true

3 Comments

also, why use ting.value rather than boxes.length? If ting.value is supposed to be less than boxes.length, you might want to check that or you'll get an array out of bounds error.
While the only value the attribute can take is checked, the DOM property is a boolean and thus takes true or false. You are suggesting setting it to the variable checked, which is undefined and thus a falsy value. The net effect is that this would have exactly the opposite effect to the one desired.
Since you have posted a correction. The string "checked" is a truthy value, so it will be converted to true. Your suggestion now has no effect beyond reducing the efficiency of the code.

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.