2

I having long list of select options predefined.

So i tried something like this when i try to edit the content

<body>
<select id="test" ....
<option value='99'>99</option>
</select>
<script language="javascript">
$(document).ready(function () {
$("select#test option[value='<?php echo $row['test']; ?>'").attr("selected", "selected");
});
</script>
</body>

php value is 99 Why this isn't working? Anything i did wrong?

3 Answers 3

2

You are lacking a ]

[value='<?php echo $row['test']; ?>'
Sign up to request clarification or add additional context in comments.

1 Comment

Oh my god, guess i need to take break.
1

You are missing the closing ']' in your selector. Change it to:

$("select#test option[value='<?php echo $row['test']; ?>']").attr("selected", "selected");

Aliter

Another way to select a value for select element is to use the val i.e

$("select#test").val('<?php echo $row['test']; ?>');

Comments

1

This is usually how I do it:

<select id="test">
    ...
    <option value="99">99</option>
    ...
</select>
<script>$("#test.val("<?php echo $row['test']; ?>");</script>

Basically, setting the value of the <select> element chooses the right option for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.