jquery - get content and attributes.

jquery contains powerful methods for manipulating html elements and attributes.

jquery get content methods

jquery html() method

this method is used to get the html contents of the first element in the set of matched elements.

this method does not accept any arguments.

this method is not available on xml documents.

the jquery html method syntax :

  $( selector ).html() ;

The jquery html method example

      $("button.last").click(function(){
                   
                   alert("current content :--"+$("p").html());                                     
                       
                        });
     

note:- in html document,html() method selector expression matches more than one element,only the first match will have its html content returned.


jquery text() method

this method is used to get the combined text contents of each element in the set of matched elements.

this method does not accept any arguments.

it can be used in both xml and html documents.

the jquery text method syntax :

  $( selector ).text() ;

The jquery text method example

      $(document).ready(function(){
           
           $("button").click(function(){
           
   alert("the li text values:------"+$("div.bg_color>ul>li").text());
   
            });

 }); </script>

note:- the .text() method cannot be used on form inputs.


jquery val() method

this method is used to get the current value of the first element in the set of matched elements.

this method does not accept any arguments.

the jquery val method syntax :

  $( selector ).val() ;

The jquery val method example

   $(document).ready(function(){
                   
                   $("button:first").click(function(){
                   
                   alert("the input field name value:--"+
                   
 $("div input:text").val("my name is sandeep"));});
 
                   $("button:last").click(function(){
                   
                   alert("the input field password value:---"+
                   
 $("div input:password").val("sandeep"));
 
                        }); });     
     

jquery get attributes - attr() method

the jquery attr() method is used to get attribute values.

the jquery get attribute attr method syntax :

  $( selector ).attr(  attribute  ) ;

attribute: specifies the name of the attribute.

the jquery attr method example

     $(document).ready(function(){
                 
                 $("button").click(function(){
                 
                 alert("the image width :--"+$('img.img').attr("width")+

        "the image height :--"+$('img.img').attr("height")); });

 });