Click here to Skip to main content
15,304,276 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I am unable to select dropdown value when i created it dynamically.

var ddlValidation = $("<select id='ddlValidation'/>");
 $(ddlValidation ).append("<option value=1>" + "Value1" + "</option>");
$(ddlValidation ).append("<option value=2>" + "Value2" + "</option>");
$(ddlValidation ).append("<option value=3>" + "Value3" + "</option>");
$(ddlValidation ).append("<option value=4>" + "Value4" + "</option>");

$(ddlValidation ).val(2);

Here i am unable to select Value2.

Please suggest.
Updated 27-May-16 2:13am

try this, tested working..
No need to apply jquery to an existing jquery object

<html lang="en-US">
    <script src="jquery.min.js"></script>
        $(function () { 
            var ddlValidation = $("<select id='ddlValidation'/>");
            ddlValidation.append("<option value=1>" + "Value1" + "</option>");
            ddlValidation.append("<option value=2>" + "Value2" + "</option>");
            ddlValidation.append("<option value=3>" + "Value3" + "</option>");
            ddlValidation.append("<option value=4>" + "Value4" + "</option>");

     <div id="divmain">
$(ddlValidation ).val('2');
Prasad Avunoori 19-May-16 10:24am
Thank you @F-ES Sitecore.

But, This is didn't work.
F-ES Sitecore 19-May-16 11:01am
It worked for me. Obviously I made the assumption that you were adding the select to the page after you'd created it, it's pointless posting only a small snippet of code and not explaining the problem or the context and if a solution doesn't help you just say it "doesn't work".

You'll need to explain in more detail.
Anil_goswami 6-Jun-16 2:01am
This worked for me too !. You should post more detailed code. Problem must be somewhere else in code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900