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,debitcar d,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
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,debitcar
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
same solution using jQuery
see
https://jsfiddle.net/HainKurt/sqa1vkug/
$(options).each(function(k, v) {
$("#Payment").append(new Option(v));
});
see
https://jsfiddle.net/HainKurt/sqa1vkug/
ASKER
Hi Experts,
I am unable to get the dropdown values.
Look at my code and suggest what to do.
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]);
}
}
what is b here?
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...
b.getElementsByTagName("biller");
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
Run the above then use F12 to get to the console - exapand the ouput for billervalue and post a screen shot here
alert('hello billervalue '+billervalue);
Do thisconsole.log(billervalue)
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
ASKER
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 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"
ASKER
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/
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.
Can you show us the XML that you are using to generate the list.
How are the initial values in the drop down being loaded ?