0

I am trying to have one of the values in my list selected. I also have a hidden field which I have to match the values in the option of the selection. If it finds one then this has to be selected.

This is the code I have.Hidden field html code.

   <input type="hidden" id="supp" name="supp" value="<?php echo $_POST['suppliers'];?>" />

My js:

<script type="text/javascript">
$(document).ready(function() {
var addSelected = $('#supp').val();
$("#suppliers option[value='" + addSelected + "']").attr("selected","selected");

console.log(addSelected);
});

</script>

The code above is not working.

3
  • put your code in document ready Commented Apr 25, 2013 at 10:03
  • Are you getting some error on the javascript console? What is the value of addSelected? Commented Apr 25, 2013 at 10:03
  • I am a getting the console value which is dynamic Commented Apr 25, 2013 at 10:06

2 Answers 2

1

Here's a jsFiddle that shows a better way to do this: http://jsfiddle.net/ATJvv/

You can just call .val on the <select> itself.

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

1 Comment

It is working. Change the value of the #supp hidden input and click run. The select will have that option chosen.
0

try this:

$("#suppliers option").each(function(index, element){
     element.selected = element.value === addSelected;
});

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.