I have an html page with a select input that is being used to filter a getJSON request in a function. In the success function of the request, I am dynamically creating an html table from the JSON and appending the table to a div in the html document. All is working fine, but the select input resets back to the default upon calling the function.
I am trying to set the select input back to the selected value so the end user know what data they are looking at. I have the class name of the select input stored in a parameter called p1Name and the selected value stored in a parameter called p1.
Normally I would use something like this: $('.ddlUserID').Val(p1);
where the class name is hard coded in. But I need this function to be dynamic and need to use the parameter of p1Name for the class name. So I have tried:
var DDL1 = "'." + p1Name + "'";
$(DDL1).val(p1);
This returns the following error, Uncaught Error: Syntax error, unrecognized expression: '.ddlUserID'. I have also tried the javascript syntax of document.getElementsByClassName(DDL1).value = p1;
which returns the same error.
If I change the parameter of DDL1 to the hard coded value of '.ddlUserID' the form works as expected.
What is the proper way to set the selected value of a select input when using a parameter as the id or class name?