-2

I am using the following lines to select the value of an option for a select menu element.

var chap = $("#xyz).val();
$('#qEchap').val(chap);

or

var chap = $("#xyz).val();
$('#qEchap option[value=chap]').attr("selected", "selected");

Both are not working. What is the right syntax to set option value for a HTML select menu?

react_or_angluar
  • 1,568
  • 2
  • 13
  • 20
coderatlarge
  • 577
  • 3
  • 8
  • 23
  • 2
    [What Do You Mean “It Doesn’t Work”?](https://meta.stackexchange.com/q/147616/289905) Where’s your HTML? There’s an obvious syntax error there: you’re missing a `"` in each code block in this post. Please post a [mre]. The problem could be anything, e.g. [Why does jQuery or a DOM method such as `getElementById` not find the element?](https://stackoverflow.com/q/14028959/4642212), or a typo, or something else. – Sebastian Simon Dec 27 '20 at 05:51
  • Tell me one language that uses this as correct syntax. ("#xyz) – react_or_angluar Dec 27 '20 at 06:22
  • sorry guys for all the confusion.. the code was too long and I could not paste everything ... the real issue was there was an Ajax call that was overwriting this statement... I moved this code within success of Ajax and it worked – coderatlarge Dec 27 '20 at 06:56

1 Answers1

1

Hope you read jQuery docs carefully to get acquainted with syntaxes.

var chap = $("#xyz").val();
$('#qEchap').val(chap);

var chap = $("#xyz").val();
$('#qEchap option[value=chap]').attr("selected", true);
qafoori
  • 781
  • 8
  • 13