homepage Welcome to WebmasterWorld Guest from 54.196.24.103
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Issue reading Listbox values
Balaji1980




msg:4249606
 11:44 pm on Jan 5, 2011 (gmt 0)

Hello Everyone,

I am having issues reading values from a listbox. Basically, I am trying to get all the values from "PickList" box. The PickList field is filled from another list box without any issue. Below is the HTML and Javascript for the PickList issue.

HTML :-
<select name="PickList" multiple id="PickList" style="width:200px;height:200px"></select>

JS :-
var pickList = document.getElementById("PickList");
for(var i = 0; i < pickList.options.length; i++)
{
alert(pickList.options[i].value);
}


The issue is the alert message always gives blank value but loops correctly. For eg. if the PicList has 4 values, then I get 4 blank alert message but not the values..!

Can someone tell me where the issue is? Thanks in advance.

 

alias




msg:4249780
 11:02 am on Jan 6, 2011 (gmt 0)

How do your options look like? Do they actually have a 'value'? E.g. <option>Lorem ipsum</option> versus <option value='someValue'>Lorem ipsum</option> - that is the only explanation I see to 'blank' values.

Fotiman




msg:4249853
 3:02 pm on Jan 6, 2011 (gmt 0)

Also, how is the PickList field "filled from another list box"?

Balaji1980




msg:4249943
 6:12 pm on Jan 6, 2011 (gmt 0)

Thanks for the replies....

The innerHTML of the PickList looks like this after adding 2 values:

<OPTION value="">ABC</OPTION><OPTION value="">DEF</OPTION>

The values are populated from SelectList listbox...below is the code.

var selectList = document.getElementById("SelectList");
var selectIndex = selectList.selectedIndex;
var selectOptions = selectList.options;
var pickList = document.getElementById("PickList");
var pickOptions = pickList.options;
var pickOLength = pickOptions.length;
if (selectIndex > -1)
{
pickOptions[pickOLength] = new Option(selectList[selectIndex].text);
pickOptions[pickOLength].value = selectList[selectIndex].value;
selectOptions[selectIndex] = null;
}

Fotiman




msg:4249945
 6:18 pm on Jan 6, 2011 (gmt 0)

Instead of this:

pickOptions[pickOLength] = new Option(selectList[selectIndex].text);
pickOptions[pickOLength].value = selectList[selectIndex].value;

Try doing this:


pickOptions[pickOLength] = new Option(selectList[selectIndex].text, selectList[selectIndex].value);

Balaji1980




msg:4249946
 6:27 pm on Jan 6, 2011 (gmt 0)

Still same result..!

<OPTION value="">ABC</OPTION>
Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved