JavaScript getAttribute() Method

In JavaScript getAttribute() method is used to get the value of an attribute in an object.

Syntax: object.getAttribute(attributename)

Parameter: It takes only one parameter attributename

Example:

Suppose you have an anchor tag with following attributes

<a href="http://www.advancesharp.com"
     id="hlnkWebsite"
     target ="_blank"
     title= "Advance Sharp"> Advance Sharp</a>

JavaScript to get the different attribute values:

<Script>
   //First get the object of which you want to get the attribute(s)
    var obj = document.getElementById(“hlnkWebsite”);

   // To get the id
    var id = obj. getAttribute("id");   //it will give  hlnkWebsite 

  // To get the target
    var id = obj. getAttribute("target");   //it will give  _ blank 

  // To get the title
    var id = obj. getAttribute("title");   //it will give  Advance Sharp

</Script>
Alicia Gonzalez Started career with .Net, and not working on client side technologies like angular, React etc. since last 12 years. But C# is really amazing language and I like to learn everything about C# and want to share whatever I know.
  • javascript
By Alicia Gonzalez On 02 Dec, 12  Viewed: 708

Other blogs you may like

A handy tutorial on creating Page Load Progress Bar using Pace.js

A website isn't complete without the inclusion of all the right elements. Progress bar is one of the key components of every website. It informs the user about the total waiting time for a particular web page or a specific task to load. Apart from adding value to the website, the progress bar is... By Celin Smith   On 18 Feb 2015  Viewed: 4,987

JavaScript isNaN function and usage

In JavaScript we have a function isNan (Not a number) to check whether the value is a number or not. It takes only one parameter, value and returns true or false, we will check here some values and conditions to understand it better. It supports in all browsers. isNaN(123); // false --... By Jason Olivera   On 30 Mar 2013  Viewed: 539

JavaScript Array: Create, Aceess and Methods

ArrayList and JavaScript, How to declare and store an ArrayList inside JavaScript, create an array in javascript, add item to javascript array, delete an item in an array in javascript, find an item in an array in javascript, these are the very basic question which most of the new developer... By Hamden   On 30 Dec 2012  Viewed: 849

Use JavaScript to hide and show text and graphics

You can show and hide the content to get attention of the user on a web page. it's a very useful thing for web designer and web developers. You can use JavaScript and CSS to achieve it. I am going to show some examples of it. There are many libraries available on internet having the feature to... By Hamden   On 30 Dec 2012  Viewed: 785

How to retrieve data from database using JavaScript in asp.net

I was checking how to validate a user email from database without post back, using JavaScript. After some search and RND found the way to call the web service method with the help of Ajax and JavaScript. Let’s say we have a registration page on which we accept email from the user and we expect... By Ali Adravi   On 24 Jun 2012  Viewed: 14,721