[development] jQuery/javascript - i need help/examples

Seth Freach sfreach at gmail.com
Fri Sep 4 16:08:57 UTC 2009


Earnie,

If you have a simple form as follows:
<form>
 <input type="text" id="alpha" class="check-on-change" />
  <input type="text" id="beta" class="check-on-change" />
  <input type="text" id="gamma" />
</form>

to do a simple auto population of the third field based on the sum of 
the first two with jquery would look like this:
<javascript>
$(document).ready(function() {
  $('.check-on-change').change(show_sum);
});

function show_sum() {
  var a = $('#alpha").val();
  var b = $('#beta").val();
  $('#gamma').val(a + b);
}
</javascript>

In jQuery, you access DOM elements with "selectors", which are strings 
that follow the exact same syntax as CSS, but wrapped in "$(...)".  Once 
you have accessed them like this, you can perform functions on them such 
as .val() with no params to /get/ the current form field value, or 
val(x) with a parameter to /set/ the value.

the first bit, "$(document).ready(function(){...});, set's everything up 
to work after the DOM has been loaded.

see http://docs.jquery.com/Core, and in particular the section on the 
left of that page titled "API Reference" for lots more info.

Seth



Earnie Boyd wrote:
> I have a form in which after two fields (x and y) are entered I need 
> default values for other fields on the form to be set.  I have limited 
> experience with javascript and none with jQuery.  I have searched for 
> examples but I can't find any for this scenario.  Can someone please 
> point me in the right direction?
>
> TIA,
> -- 
> Earnie
> -- http://r-feed.com/           -- http://for-my-kids.com/
> -- http://www.4offer.biz/       -- http://give-me-an-offer.com/
>
>


More information about the development mailing list