1

i have this function which loops through an array of check boxes checking if the boxes value is equal to something in a text box, i dont know whats wrong.

function checkValue(contain)  {  
        var boxes = document.getElementsByTagName("input");  
    for (var i = 0; i < boxes.length; i++) {  
        if (boxes[i].name == "vote[]") {  
            if (boxes[i].value.indexOf(contain.value) != -1) {  
            boxes[i].checked = true;  
        }  
    }   
}  
}

and this is how i call it

 OnClick="uncheckAll(); checkValue(document.getElementsByName("countrylist"));"

this code is in side a echo in php which is like this echo ' ';

4 Answers 4

2

You cannot nest the same type of quotes in HTML.

OnClick="uncheckAll(); checkValue(document.getElementsByName(\'countrylist\'));"

Simply escape the single quotes as shown as PHP provides an easy escape mechanism.

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

2 Comments

as said, im using php to echo it in single quotation marks so if i were to use single quotation marks my php would error
@Ryan, then escape them with \' php.net/manual/en/language.types.string.php
0

the contain argument you're passing is an array not a string. that could be the problem?

3 Comments

but the value "contain" is not an array its the word "UK"
the argument "contain" is the return value of this call document.getElementsByName("countrylist") getElementsByName returns an array of elements with that name
yes, or you should pass document.getElementsByName("countrylist")[0] to the function. either way should help. also, as InsDel suggested, you can't nest javascript double quotes inside html double quotes, so change them for single quotes document.getElementsByName('countrylist')[0]
0

I think your javascript might be having an issue with the name of your field being vote[].

Does it ever pass the condition:

if (boxes[i].name == "vote[]") {

1 Comment

yes, i have a function to check all boxes so i modified it to the above results, but yes the check all function works
0

1) Update the inlince call to use single quotes:

checkValue(document.getElementsByName('countrylist'))

2) Use document.getElementsByName to make the function little better:

function checkValue(contain)  
{           
    var boxes = document.getElementsByName("vote[]");
    for (var i = 0; i < boxes.length; i++) 
    {     
        boxes[i].checked = (boxes[i].value == contain.value);
    }   
} 

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.