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:
Hi,

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

Java
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.
Posted
Updated 27-May-16 2:13am
v2

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

HTML
<html lang="en-US">
<head>
    <script src="jquery.min.js"></script>
      <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>");
            ddlValidation.val(2);
            $('#divmain').append(ddlValidation);
        });
    </script>
</head>
<body>

     <div id="divmain">
     </div>
</body>
</html>
   
$(ddlValidation ).val('2');
   
Comments
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