Setting form values in jQuery

For my assignment in “Visual Databases”, one of my projects involved having a hidden form value set using Javascript. The value would be set to the id of the div when the mouse moved over one of the div elements.

Since I was using jQuery, I looked around for some code that would enable me to do this using jQuery, and this was the result:

Javascript (in <head>):

 $(document).ready(function(){
   // other functions
   $("div.photo").mouseover(function() {
     $("div.photo").css({backgroundColor:"white"});
     $(this).css({backgroundColor:"blue"});
     var idval = $(this).attr("id");
     $("input#pcompid").val(idval);
   });
 });

HTML in body:

<form action="">
<input type="hidden" id="pcompid" name="pcompid" value="">
<input type="submit" value="Compare">
</form>

<div class='photo' id='2419405422'>
<a href="morephotos.php?photoid=2419405422">
<img border='0'>
</a></div>

1 Response to “Setting form values in jQuery”


  1. 1 pfwd June 30, 2009 at 12:49 pm

    Thanks. Just what I needed


Leave a Reply