jQuery set and get form element values

August 2nd, 2012 by Laeeq | 2 comments

jQuery is a Javascript framework which simplify Javascript coding complexity and also fixed cross browser issues. You can get and set html form element values by using  jQuery.

Getting a form value with jQuery

jQuery uses val() function to get a form values. Below is the example form with id city.

  1. <form>
  2. <input name=“city” id=“city” type=“text”>
  3. </form>

Now we will get the value from id “city” and display an alert:

  1. window.alert( $(‘#city’).val() );

If you’re using the name only and not specifying an id, the jQuery to get the form values would be this:

  1. window.alert( $(‘[name=city]‘).val() );

Setting a form value with jQuery

You can easilly set the form values with jQuery using the same val() function but passing it a new value. Using the same example forms above, you can do this for the text input.

  1. $(‘#city’).val(‘city value’);

OR

  1. $(‘[name=city]‘).val(‘city value’);

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • Use Google Hosted JQuery and Javascript Libraries in Your Projects
  • jQuery check and uncheck a checkbox
  • Popular Free HTML5 and CSS Templates
  • Awesome Responsive CSS Frameworks
  • Really awesome jQuery Plugins and Tutorials
  • Responsive JQuery Slider Plugins
  • Useful jQuery Plugins for improving Typography
  • Here We will Show popup message for few seconds using jQuery
  • Ajax – Asynchronous JavaScript and XML
  •  

     

    1. joserogersfx
      October 3rd, 2012 at 09:28 | #1

      hi!,I like your writing so a lot! share we communicate extra about your post on AOL? I require an expert on this area to solve my problem. May be that’s you! Having a look ahead to see you.

    2. October 7th, 2012 at 08:59 | #2

      Ahaa, its pleasant conversation regarding this paragraph here at this web site, I have
      read all that, so at this time me also commenting here.

    1. No trackbacks yet.