views:

113

answers:

1

Hi all,

I'm trying to redo this in jQuery:

<script language="JavaScript" type="text/javascript">
  var thisIsSelected = '<?php echo $_POST['image']; ?>';
  var sel1= document.getElementById('image');
  sel1.value = thisIsSelected;
</script>

But I seem to keep breaking it lol

Basically I want jQuery to check if $_POST['image'] exists and if so make it selected on the form. I'm assuming if it's possibly with javascript, jquery can do it easier ;)

+2  A: 

You can set the value using .val() in jQuery, like this:

$(function() {
  $('#image').val("<?php echo $_POST['image']; ?>");
});

The $(function() { }); wrap is so the code executes on document.ready, when all the elements are there and ready. If the value doesn't exist in the <option> list, the .val() will just have no effect.

Nick Craver
thank you muchly! :)
SoulieBaby