0

I am trying to pass a simple .js variable (prodID) into a jquery function that preselects a dropdown. I know the prodID variable has a value because document.write prints it out. I know that the jQuery works, because when I assign an arbitrary value (say....225) to the 'value =' variable, the dropdown selects as it should. I just can't get value='prod_id' to work. Is always null. What am I doing wrong?

    var ProductID = "ProductID";
    var prodID = getCookieValue(ProductID);
    document.write(prodID);

    changeDropDown(prodID); 

    function changeDropDown(prodID){
        jQuery("#groupsel_0 option[value= 'prodID' ]").prop("selected", true);
        jQuery('#groupsel_0').trigger('change');  
    };

I have tried not wrapping in a function too.

2
  • 1
    Maybe jQuery("#groupsel_0 option[value= "+prodID+" ]").prop("selected", true); ? Commented Oct 9, 2013 at 19:37
  • Insert it into the string the same way you would insert any other javascript variable into a string. "foo" + myvar + "bar" Commented Oct 9, 2013 at 19:44

1 Answer 1

1
function changeDropDown(prodID){
    jQuery("#groupsel_0 option[value= '" + prodID + "']").prop("selected", true);
    jQuery('#groupsel_0').trigger('change');  
};

you need to escape your string

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.