Sunday, 27 October 2013

JQuery guide for beginner’s part 8 – Accessing the attributes of HTML Elements

HI, today we are going to learn access the html elements when the DOM is ready, when we working on jquey this is very important know accessing attributes of html elements with that we can add some more interactivity in cross-browser. We can access the attributes of html elements using .attr() method. Let’s see bellow examples.

Ex:

$(document).ready(function(){

$(‘.button’).click(function(){

Var myid = $(‘#textbox’).attr(‘id’);

alert(myid);

});

});HTML Code

<input class="button" type="button" />

The above example gives the #textbox id when we click on .button, here we assign a click event to .button as a selector and we assign .attr() method to #textbox to know the ID of textbox.

I am going to show one more example, with that you can get more idea on accessing the html elements.

The above example gives the #textbox value when we click on .button, here we assign a click event to .button as a selector and we assign .val() method to #textbox to know get the value of textbox, .val() method is used to get and set the values of html elements.