Home > jquery > Setting form values in jQuery

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);
   });
 });

What the above code does is : for a mouseover on a div.photo, it sets the background color, and then gets the “id” attribute of that div – and sets the form value (input#pcompid) to that value.

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>

Categories: jquery
  1. June 30, 2009 at 12:49 pm | #1

    Thanks. Just what I needed

  2. February 12, 2010 at 4:07 pm | #2

    good code!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.