Jquery Properties



We can manipulate some of the most basic components when it comes to dom elements, those elements have the assigned properties and qualities.

Most of these features are available in the form of dome nodes properties through javascript. There are some more common properties -
  • className
  • tagName
  • id
  • href
  • title
  • rel
  • src
<img id = "imageid" src = "image.gif" alt = "Image" class = "myclass" 
   title = "This is an image"/>

Get property values

The Attr () method can be used to either match the set or set attribute values in the value of an attribute from the first element.

Examples

The following is a simple example, which gets the title attribute with <em> tag and set <div id = "split"> same value -

<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
  
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            var title = $("em").attr("title");
            $("#divid").text(title);
         });
      </script>
   </head>
 
   <body>
      <div>
         <em title = "Bold and Brave">This is first paragraph.</em>
         <p id = "myid">This is second paragraph.</p>
         <div id = "divid"></div>
      </div>
   </body>
</html>
Result
This is first paragraph.
This is second paragraph.
Bold and Brave

Set property values
Attr (name, value) method can be used to set the named attribute on all elements in a wrapped set using the passed value.

Examples
The following is a simple example that sets the src attribute of an image tag at a right place -

<html>
   <head>
      <title>The jQuery Example</title>
      <base href="https://www.tutorialspoint.com" />
      <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
  
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            $("#myimg").attr("src", "/jquery/images/jquery.jpg");
         });
      </script>
   </head>
 
   <body>
      <div>
         <img id = "myimg" src = "/images/jquery.jpg" alt = "Sample image" />
      </div>
   </body>
</html>

Attribute Methods

Following table lists down few useful methods which you can use to manipulate attributes and properties −
Sr.No.Methods & Description
1attr( properties )
Set a key/value object as properties to all matched elements.
2attr( key, fn )
Set a single property to a computed value, on all matched elements.
3removeAttr( name )
Remove an attribute from each of the matched elements.
4hasClass( class )
Returns true if the specified class is present on at least one of the set of matched elements.
5removeClass( class )
Removes all or the specified class(es) from the set of matched elements.
6toggleClass( class )
Adds the specified class if it is not present, removes the specified class if it is present.
7html( )
Get the html contents (innerHTML) of the first matched element.
8html( val )
Set the html contents of every matched element.
9text( )
Get the combined text contents of all matched elements.
10text( val )
Set the text contents of all matched elements.
11val( )
Get the input value of the first matched element.
12val( val )
Set the value attribute of every matched element if it is called on <input> but if it is called on <select> with the passed <option> value then passed option would be selected, if it is called on check box or radio box then all the matching check box and radiobox would be checked.






Share on Google Plus

About It E Research

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

1 comments:

  1. I will recommend anyone looking for Business loan to Le_Meridian they helped me with Four Million USD loan to startup my Quilting business and it's was fast When obtaining a loan from them it was surprising at how easy they were to work with. They can finance up to the amount of $500,000.000.00 (Five Hundred Million Dollars) in any region of the world as long as there 1.9% ROI can be guaranteed on the projects.The process was fast and secure. It was definitely a positive experience.Avoid scammers on here and contact Le_Meridian Funding Service On. lfdsloans@lemeridianfds.com / lfdsloans@outlook.com. WhatsApp...+ 19893943740. if you looking for business loan.

    ReplyDelete