Link to home
Start Free TrialLog in
Avatar of srikotesh
srikotesh

asked on

how to populate dropdown selected value using javascript

Hi Experts,

Dropdown name Payment having 4 values:

creditcard
debitcard
cash
cheque

these are not static i am getting from DB.
SOME TIME IT MAY HAVE 3 VALUES(creditcard,debitcard,cash) OR 4 VALUES(ALL THE VALUES FROM ABOVE MENTIONED)

I AM DOING SAVE OPERATION SUCCESSFULLY.
WHEN I DO VIEW OPERATION
DROP DOWN SHOULD DISPLAY THE WHAT I HAVE SAVED EARLIER AND REST OF THE VALUE SHOULD  DISPLAY AFTER THAT SELECTED VALUE

FOR EXAMPLE:
IF I SAVED DEBITCARD VALUE AS PAYMENT

WHILE VIEW/EDIT PAGE DROPDOWN SHOULD DISPLAY:

debitcard
cash
cheque
creditcard

can some one suggest how to do using java script for loop iteration
Avatar of zephyr_hex (Megan)
zephyr_hex (Megan)
Flag of United States of America image

What server side code are you using ?  (PHP, C#, or ?) ?

How are the initial values in the drop down being loaded ?
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
same solution using jQuery

$(options).each(function(k, v) {
  $("#Payment").append(new Option(v));
});

Open in new window


see
https://jsfiddle.net/HainKurt/sqa1vkug/
Avatar of srikotesh
srikotesh

ASKER

Hi Experts,

I am unable to get the dropdown values.

Look at my code and suggest what to do.
		function updatebiller()
		{
			var obj = document.obpresentform;
			var billervalue = b.getElementsByTagName("biller");
			//values i am getting from db thru xml framework,inthis step values are coming but i am unable to get it from the var billervalue
			alert('hello billervalue '+billervalue);  // o/p of this alert : [object html collection]			
			billervaluelen = billervalue.length;	//o/p:3		
			var bilcatarray = billervalue.split(",");//o/p: billervalue.split is not a function
			alert(obj.payment.options[obj.payment.selectedIndex].text);//o/p: i am getting selected value as debitcard
						
			var payment = obj.payment.options[obj.payment.selectedIndex].text;			
			
			for (i = 0; i < billervaluelen; i++) 
			{
			  var option = document.createElement("option");
			  option.text = bilcatarray[i];
			  alert('vals '+bilcatarray[i]);
			  payment.add(option);
			  payment.add(option, payment[0]);
			}
		}					
			

Open in new window

what is b here?

b.getElementsByTagName("biller");

Open in new window


can you create a demo on jsFiddle.net, like the one I started for you

https://jsfiddle.net/HainKurt/L245ckk3/

and I added some hardcoded values and commented out some lines to make it work...

https://jsfiddle.net/HainKurt/xsur9yke/

of course it is not what you need probably, but at least it is working mostly...
Instead of this
alert('hello billervalue '+billervalue);

Open in new window

Do this
console.log(billervalue)

Open in new window

And post the result here because you are using it on an object (HTML collection) - you need to apply it to a string which probably means you need to iterate over billervalue to do so - however before suggesting this I would like to see what the actual contents of billervalue is.

Run the above then use F12 to get to the console - exapand the ouput for billervalue and post a screen shot here
Hi Julian,

Hi julian,

billervalue from console log is:
(3) [biller, biller, biller]
length:3
0:biller

childNodes:
nodeValue:"debitcard"

1:biller
childNodes:
nodeValue:"creditcard"

2:biller
childNodes:
nodeValue:"cheque"
Hi Experts,

I am able to get the values and showing in the dropdown
problem is i could see one empty blank space in dropdown along with selected and rest of the dropdown values
how to remove this blank space from dropdown

Example:
debitcard(selected value)
creditcard
//here i could see empty
cheque

DO I NEED TO REDUCE THE SIZE OF THE billervaluelen?
SUGGEST ME HOW TO DO.

please refer the code here
https://jsfiddle.net/wL9x3593/
Your fiddle does not do anything if you need to post code then do so in the thread using code tags.

Can you show us the XML that you are using to generate the list.