jquery - set content attributes.

jquery contains set of powerful functions for manipulating html attributes and elements.


set content methods

jquery html(content) method.this method is used to set the html contents each element in the set of matched elements.

this method is not available on xml documents.

syntax:

     $( selector ).html(content) ;

contentspecifies the new content for the selected elements.

The jquery html method example

     $(document).ready(function(){
           
                   $("button:first").click(function(){
                   
                   $("p").html("<img src=".images\\blue.jpg"/>);
                   
                   $("button.last").click(function(){
                   
                   alert("current content :--"+$("p").html());                                     
                       
                        });}); 
     

jquery text(content)   method

this text('value') method is used to set the content of each element in the set of matched elements to the specified text.

this method accept string type one argument.

it can be used in both xml and html documents.

syntax :

$( selector ).textcontent ) ;

the jquery text method example

     <script>
          
                   $(document).ready(function(){
                   
                   $("button").click(function(){
                   
          $("div.bg_color>ul>li").text("this is text() method effect");
                   
                    }); });
                     
    </script>
     

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


jquery val(value) method

this method is used to set the value of each element in the set of matched elements.

this method accept one arguments.

syntax :

$( selector ).val(value) ;

the jquery val method example

     <script> $(document).ready(function(){
        
        $("button").click(function(){
        
        $("div input:text").val("hy my name is sandeep");
        
        $("div input:password").val("sandeep");
        
               }); });  </script>
     

set attributes - attr() method

the jquery attr(attribute , value) method is used to set one or more attribute for the set of matched elements.

syntax :

           $( selector ).attr(  attribute,  value  ) ;

attribute: specifies the name of the attribute.

value : specifies the value of the attribute.

The jquery attr method example

     $(document).ready(function(){
           
           $("button").click(function(){
                   
           $("img").attr({width:"456px", height:"214px"});
           
                        }); })