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

Thanks. Just what I needed
good code!