Loading Please Wait...

Logo Lynxsia IT Solutions

jQuery DOM Set

JQ DOM Set

jQuery contains powerful methods for changing HTML elements, attributes, and values.

DOM - Set Content

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

  • text() - set or returns the text content of selected elements
  • html() - set or returns the content of selected elements (including HTML markup or tags)
  • val() - set or returns the value of form fields
					 
        
          $("#test1").text("Hello world!");
          $("#test2").html("This is <b>bold</b> paragraph.");
          $("#input").val("Lynxsia IT Solutions");
        
      
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.

					 
        
          $("#test1").text(function(i, origText){
            return "Old text: " + origText + " New text: Hello world! (index: " + i + ")";
          });
        
      
DOM - Set Attribute

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

					 
        
          $("#link").attr('href', 'https://lynxsia.com/')
        
      

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

					 
        
          $("#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.

					 
        
          $("#w3s").attr("href", function(i, origValue){
            return origValue + "/jquery/";
          });
        
      
DOM - Set data- Attribute

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

Example
					 
        
          $("#test").data('user', 'John Doe')
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved