homepage Welcome to WebmasterWorld Guest from 54.166.255.168
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

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#: 4249604 posted 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

10+ Year Member



 
Msg#: 4249604 posted 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

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4249604 posted 3:02 pm on Jan 6, 2011 (gmt 0)

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

Balaji1980



 
Msg#: 4249604 posted 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

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4249604 posted 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#: 4249604 posted 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.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved