listbox select count not work dont work

Posted by sadpcd on Stack Overflow See other posts from Stack Overflow or by sadpcd
Published on 2012-06-19T02:38:16Z Indexed on 2012/06/19 3:16 UTC
Read the original article Hit count: 130

Filed under:
|

I recently trying to make a form with multiple select box. When someone select the options the number of selected options will be display on another text. I'm a beginner in JavaScript.

The function is called, but it doesn't count the number of the selected options.

<select name="element_17_1[ ]"
        size="7" multiple="multiple"
        class="element select medium"
        id="element_17_1[ ]"
        onfocus="selectCount(this.form);"
        onClick="selectCount(this.form);"
    >
    <option value="Opt1">Opt1</option>
    <option value="Opt2">Opt2</option>
    <option value="Opt3">Opt3</option>
    <option value="Opt4">Opt4</option>
    <option value="Opt5">Opt5</option>
    <option value="Opt6">Opt6</option>
    <option value="Opt7">Opt7</option>
</select>

and this is the function I tried in the <head>

function selectCount(f) {
    var selObj = myForm.elements['element_17_1[]'];
    var totalChecked = 0;
    for (i = 0; i < selObj.options.length; i++) {
        if (selObj.options[i].selected) {
            totalChecked++;
        }
    }
    f.element_9.value = totalChecked;
}

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html