Skip to main content

Posts

Showing posts with the label jQuery methods

Seven Most Common jQuery Methods Part Two: ATTR() and VAL()

.attr() According to jQuery , it can get the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element. So I use the img tag to add an apple image at the bottom of the page. Now I want to change the apple image to a banana image, so I select the img first, and change the "src" attribute value to a new image address.  $("img").attr("src", "the new image address") If I want to change the input type from text to color, I can write: $("input").attr("type", "color") . As the result, the input box changes to a color input. .val() According to  jQuery , this method gets the current value of the first element in the set of matched elements or set the value of every matched element. I type my name in the input, and then I call my name in the console by typing $("input").val() . If I want to update the ...

Seven Most Common jQuery Methods Part One: Text() and HTML()

The course introduced seven most common jQuery methods that can save us a lot of time and make our code super clean. .text() .html() .attr() .val() .addClass() .removeClass() .toggleClass() Today, I am going to share the first two methods study note with you. I start to create a simple HTML page includes h1, an simple input and a fruit list. Then, I link to the jQuery file and check if it is connected correctly. .text() According to jQuery,  this method can get combined text contents of each element in the set of matched elements, including their descendants, or set the text contents of the matched elements. For example, if I want to see the h1 text content in console, I just need to type $("h1").text() . Then, it will call back the h1 text. We can do the same thing to the list content, too. When I select the li, it will return all list items in a string. text() method can also change the content. I can change the h1 text...