Hi friends

I am new to javascript
I came across to use that in my application
Can anyone please tel me, how can i loop through the items in a listbox?

Thank you all

Recommended Answers

All 7 Replies

<html>
<head>
    <script>
    function loop()
    {
        var list = document.getElementById('list');
        for(var i = 0; i < list.options.length; ++i)
            alert(list.options[i].value);
    }
    </script>
</head>
<body>
    <form>
        <select name="list" id="list">
            <option value="India">India</option>
            <option value="US">US</option>
            <option value="Germany">Germany</option>
        </select>
        <br /><br />
        <input type="button" id="btn" value="Button" id="btn" onclick="loop();" />
        <div name="myDiv" id="myDiv"></div>
    </form>
</body>
</html>

Thanks for the reply
I tried that in my application as

function onChange(checkBoxRef,listboxId)
{
var listboxRef = document.getElementById(listboxId)
if(checkBoxRef.checked)
{


for (var i=0;i<listboxref.length;i++)
 
 
{

listBoxRef.options[i].selected = true;

}
}
else
{
 
for (var i=0; i<listBoxRef.options.length; i++)
 

{

listBoxRef.options[i].selected = false;

}
}
}

I am trying, so that if the checkbox is checked, all the items in listbox should get selected.
If checkbox is unchecked, all the items get deselected

But it is not working
And it is writing "Error on page" at the left bottom of the page
Can you please how to fix this?

Thank you for your time

<html>
<head>
    <script>
    function loop()
    {
        var list = document.getElementById('list');
        var chkBox = document.getElementById('chk');
        if(chk.checked)
        {
            for(var i = 0; i < list.options.length; ++i)
                list.options[i].selected = true;
        }
        else
        {
            for(var i = 0; i < list.options.length; ++i)
                list.options[i].selected = false;
        }    //alert(list.options[i].value);
    }
    </script>
</head>
<body>
    <form>
        <select name="list" id="list" size="3" multiple="multiple">
            <option value="India">India</option>
            <option value="US">US</option>
            <option value="Germany">Germany</option>
        </select>
        <br /><br />
        <input type="checkbox" id="chk" name="chk" onchange="loop();"/>
    </form>
</body>
</html>

Thank you
I got it worked

Hi

I got the javascript to work
But it lead to another issue
I am calling javascript frm server side
What happens is as the listbox 1 get populated, in my application, i have a button which refreshes the page thereby populating listbox2, with reference to the items selected in listbox1
So what is happening is,
- the user selects checkall-- all items get selected
- the user hits the refresh button -- all the items deselected and also checkbox is marked unchecked

Hope you can understand my issue
Please help me to fix this dilemma

Thank you very much for your help so far

There must be some kind of processing going on at the server side with the items selected. It very much depends on the server side language you are using. Maybe asking in the appropriate language would be better option.

Thanks for pointing out me to right forum
Thanks for all your support

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.