Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Reading and changing values

From: jQuery for Web Designers

Video: Reading and changing values

Working in jQuery there's going to be many times when you want to find out, in your code, what the contents of something you selected are. Let's look at some ways to do that now. This page has a form, as you can see, and obviously, a lot of other HTML. So first let's look at the stuff that's not in the form. I'm just going to scroll down a little bit here, and here's my header, which I happen to know is replaced by this image over here. Let's take a look at it. So, I'm going to select the header ID, like this, and then I'm going call to the HTML method on it. This will return all the HTML inside that selected element.

Reading and changing values

Working in jQuery there's going to be many times when you want to find out, in your code, what the contents of something you selected are. Let's look at some ways to do that now. This page has a form, as you can see, and obviously, a lot of other HTML. So first let's look at the stuff that's not in the form. I'm just going to scroll down a little bit here, and here's my header, which I happen to know is replaced by this image over here. Let's take a look at it. So, I'm going to select the header ID, like this, and then I'm going call to the HTML method on it. This will return all the HTML inside that selected element.

Like this. If I want to strip out all that HTML and just see the text, wouldn't you know it, there's a method called text. Now, as you can see, both of these return the HTML or the text with some white space. Depending on which browser you're using you might get differing amounts of white space. This is one area that jQuery does not smooth out for you. So it also includes a trim method that can take care of that if you want, like this. And the HTML method works bidirectionally, so I can read stuff out, and I can also write stuff back in. I'm not going to use this header for now, because it's got that image over it.

Let's look at this one. I happen to know that that one has the ID of, Page ID. So, let's select that Page ID, and now I am going to set its HTML to an h1 tag that says, hi, there we go. So, you can read from or write to any element that you've selected using these methods, that's pretty handy. Now, let's switch over to the jQuery code that's at the bottom of this file And, look at working with a web form. There's is a different set of methods that are used for this application. If you have ever worked with a web form before, you know that getting values out of certain form type can be a little bit of a pain or at least different between different form types.

jQuery smoothes out a lot of that for you. So, here I'm listening for these submit event on this contact form. I'm stopping it from submitting and then I'm logging several values to the console using the val method. This will work on nearly any form element. So I'm writing out to the console the value of the name field, the email filed, the state field, which is a different field type but I just use the same method. And one great thing is that it also works on radio buttons, provided you give it a good selector. So if you're using regular JavaScript you'd have to loop over these and figure out which one is checked and only then could you get the value.

In jQuery you can use a clever selector and do it all on one line. So, in this case, I'm selecting input and I'm using the radio filter, which is built into jQuery, and then, beyond that, I'm looking for a set of radio buttons where their name is station2, which, if you look in the HTML, is what these are, and then, I'm looking for the one that's checked. And then I'm using the same val method on this multiselect, and this works on all of them. So let's check couple of things, open the console and submit this form. There we have it. All these values are coming out. And this is interesting.

I'm getting an array back from my multi-select. So, I can get all of these values, or just the one. Try this again, there we go. It's still an array, because it was a multi-select, but it just has the one item in there. And just like with the HTML and text methods, you can use val to write values back into these fields. So let's try that here in the console. Let's say I want to modify that value of that email field. Let's make it jim@example.net. I'll scroll up so you can see this and once I hit return, wowie zowie, it changes and this works on many other field types. So I can do this on the state field.

Be careful when you're doing this because you want to make sure that the value matches the actual value attributes and in this state field, I scroll up. You can see that the values are state codes, not the spelled out version. So, were going to set the value here to NJ for New Jersey, and there it is. And finally we know that we get an array out of your favorite magazine. Can we put an array back in? Let's find out. So I'm going to use square braces for my array and then I'm going to set the values, and again you need to be careful of the values that are displayed here and the ones that are shown because this is a select box again.

I know that these are all the same thing, just with the spaces removed, so I'm going to set the value to mountain biking and modern cyclist. There we go, you can also do this just with a string or a single value just like that. If you've ever worked with a check box you know that checking the value is not sufficient with a check box because the value doesn't change to checked or unchecked depending on the state of the check box. There's another way to work with check boxes that we'll look at next.

Show transcript

This video is part of

Image for jQuery for Web Designers
jQuery for Web Designers

37 video lessons · 13358 viewers

Joe Chellman
Author

 
Expand all | Collapse all
  1. 2m 17s
    1. Welcome
      1m 4s
    2. What you should know
      37s
    3. Using the exercise files
      36s
  2. 12m 0s
    1. jQuery is a JavaScript library?
      1m 15s
    2. When to use jQuery
      2m 51s
    3. Alternatives to jQuery
      1m 25s
    4. Which version of jQuery to use
      1m 50s
    5. How to install jQuery
      3m 21s
    6. Reference materials
      1m 18s
  3. 19m 19s
    1. Getting ready
      2m 26s
    2. Selecting elements to use
      3m 54s
    3. Performing multiple operations on the same line with chaining
      2m 30s
    4. Using classes to find what you're looking for
      3m 52s
    5. Adding, modifying, and removing content dynamically
      4m 3s
    6. Challenge: Form feedback
      1m 12s
    7. Solution: Form feedback
      1m 22s
  4. 18m 28s
    1. Triggering a change based on activity with event binding
      4m 37s
    2. Reading and changing values
      4m 17s
    3. Working with HTML attributes
      4m 55s
    4. Challenge: Dynamic contact form
      1m 27s
    5. Solution: Dynamic contact form
      3m 12s
  5. 16m 42s
    1. Using the Colorbox plugin to build a slideshow gallery
      4m 22s
    2. Implementing Colorbox on your site
      2m 46s
    3. Changing Colorbox options
      5m 53s
    4. Challenge: Convert to a slideshow
      1m 6s
    5. Solution: Convert to a slideshow
      2m 35s
  6. 28m 37s
    1. Using jQuery or CSS to animate elements
      2m 24s
    2. Creating simple jQuery animations
      4m 35s
    3. Animating numeric properties with animate()
      4m 56s
    4. Understanding animation easing
      4m 4s
    5. Putting it together: Flowers in the cart
      6m 4s
    6. Callbacks: What to do when the animation ends
      3m 27s
    7. Challenge: Improve the animation
      1m 3s
    8. Solution: Improve the animation
      2m 4s
  7. 4m 45s
    1. More fun plugins
      2m 38s
    2. What's next: More jQuery
      50s
    3. What's next: More JavaScript
      1m 17s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed jQuery for Web Designers.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.