Set attr html jquery

jQuery — Set Content and Attributes

We will use the same three methods from the previous page to set content:

  • text() — Sets or returns the text content of selected elements
  • html() — Sets or returns the content of selected elements (including HTML markup)
  • val() — Sets or returns the value of form fields

The following example demonstrates how to set content with the jQuery text() , html() , and val() methods:

Example

A Callback Function for text(), html(), and val()

All of the three jQuery methods above: text() , html() , and val() , also come with a callback function. The callback function has two parameters: the index of the current element in the list of elements selected and the original (old) value. You then return the string you wish to use as the new value from the function.

The following example demonstrates text() and html() with a callback function:

Example

$(«#btn1»).click(function() <
$(«#test1»).text(function(i, origText) <
return «Old text: » + origText + » New text: Hello world!
(index: » + i + «)»;
>);
>);

$(«#btn2»).click(function() $(«#test2»).html(function(i, origText) return «Old html: » + origText + » New html: Hello world!
(index: » + i + «)»;
>);
>);

Set Attributes — attr()

The jQuery attr() method is also used to set/change attribute values.

The following example demonstrates how to change (set) the value of the href attribute in a link:

Example

The attr() method also allows you to set multiple attributes at the same time.

The following example demonstrates how to set both the href and title attributes at the same time:

Читайте также:  Description of c sharp

Example

$(«button»).click(function() <
$(«#w3s»).attr( <
«href» : «https://www.w3schools.com/jquery/»,
«title» : «W3Schools jQuery Tutorial»
>);
>);

A Callback Function for attr()

The jQuery method attr() , also comes with a callback function. The callback function has two parameters: the index of the current element in the list of elements selected and the original (old) attribute value. You then return the string you wish to use as the new attribute value from the function.

The following example demonstrates attr() with a callback function:

Example

$(«button»).click(function() <
$(«#w3s»).attr(«href», function(i, origValue) <
return origValue + «/jquery/»;
>);
>);

jQuery Exercises

jQuery HTML Reference

For a complete overview of all jQuery HTML methods, please go to our jQuery HTML/CSS Reference.

Источник

.attr()

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.

Contents:

.attr( attributeName ) Returns: String

Description: Get the value of an attribute for the first element in the set of matched elements.

version added: 1.0 .attr( attributeName )

The .attr() method gets the attribute value for only the first element in the matched set. To get the value for each element individually, use a looping construct such as jQuery’s .each() or .map() method.

Using jQuery’s .attr() method to get the value of an element’s attribute has two main benefits:

  1. Convenience: It can be called directly on a jQuery object and chained to other jQuery methods.
  2. Cross-browser consistency: The values of some attributes are reported inconsistently across browsers, and even across versions of a single browser. The .attr() method reduces such inconsistencies.

Note: Attribute values are strings with the exception of a few attributes such as value and tabindex.

As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. To retrieve and change DOM properties such as the checked , selected , or disabled state of form elements, use the .prop() method.

Читайте также:  Css overflow hidden dots

Attributes vs. Properties

The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.

For example, selectedIndex , tagName , nodeName , nodeType , ownerDocument , defaultChecked , and defaultSelected should be retrieved and set with the .prop() method. Prior to jQuery 1.6, these properties were retrievable with the .attr() method, but this was not within the scope of attr . These do not have corresponding attributes and are only properties.

Concerning boolean attributes, consider a DOM element defined by the HTML markup , and assume it is in a JavaScript variable named elem :

elem.checked true (Boolean) Will change with checkbox state
$( elem ).prop( «checked» ) true (Boolean) Will change with checkbox state
elem.getAttribute( «checked» ) «checked» (String) Initial state of the checkbox; does not change
$( elem ).attr( «checked» ) (1.6+) «checked» (String) Initial state of the checkbox; does not change
$( elem ).attr( «checked» ) (pre-1.6) true (Boolean) Changed with checkbox state

According to the W3C forms specification, the checked attribute is a boolean attribute, which means the corresponding property is true if the attribute is present at all—even if, for example, the attribute has no value or is set to empty string value or even «false». This is true of all boolean attributes.

Nevertheless, the most important concept to remember about the checked attribute is that it does not correspond to the checked property. The attribute actually corresponds to the defaultChecked property and should be used only to set the initial value of the checkbox. The checked attribute value does not change with the state of the checkbox, while the checked property does. Therefore, the cross-browser-compatible way to determine if a checkbox is checked is to use the property:

Читайте также:  Timeweb is here

The same is true for other dynamic attributes, such as selected and value .

Additional Notes:

  • In Internet Explorer prior to version 9, using .prop() to set a DOM element property to anything other than a simple primitive value (number, string, or boolean) can cause memory leaks if the property is not removed (using .removeProp() ) before the DOM element is removed from the document. To safely set values on DOM objects without memory leaks, use .data() .

Examples:

Display the checked attribute and property of a checkbox as it changes.

Источник

Attributes

An element’s attributes can contain useful information for your application, so it’s important to be able to get and set them.

The .attr() method acts as both a getter and a setter. As a setter, .attr() can accept either a key and a value, or an object containing one or more key/value pairs.

$( "a" ).attr( "href", "allMyHrefsAreTheSameNow.html" );
$( "a" ).attr(
title: "all titles are the same too!",
href: "somethingNew.html"
>);
$( "a" ).attr( "href" ); // Returns the href for the first a element in the document

Last Updated

Suggestions, Problems, Feedback?

Chapters

Books

Copyright 2023 OpenJS Foundation and jQuery contributors. All rights reserved. See jQuery License for more information. The OpenJS Foundation has registered trademarks and uses trademarks. For a list of trademarks of the OpenJS Foundation, please see our Trademark Policy and Trademark List. Trademarks and logos not indicated on the list of OpenJS Foundation trademarks are trademarks™ or registered® trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them. OpenJS Foundation Terms of Use, Privacy, and Cookie Policies also apply. Web hosting by Digital Ocean | CDN by StackPath

Источник

Оцените статью