Saggex
Saggex

Reputation: 3500

How to get attributes of a link?

I need to get attributes from a hyperlink like:

<a href="/groups/49882348/edit#" id="toleft" w_command="add_holiday" w_controller="groups" w_id="49882348" w_type="POST">&lt;&lt;</a>

and I want to get w_command, what do I have to do in Javascript? I tried the following:

var lin = document.getElementById("#toleft");
var w_controller = lin.attr("w_controller");

But this won't work. Any suggestions?

Upvotes: 1

Views: 56

Answers (3)

Richard Peck
Richard Peck

Reputation: 76774

@Kiranramchandran got it for javascript, but for JQuery you'll be able to do this:

var lin = $("#toleft");
var w_controller = lin.attr("w_controller");

Demo

Upvotes: 2

Kumar Akarsh
Kumar Akarsh

Reputation: 4980

Using Jquery:

console.log($('#toleft')[0].attributes['w_controller']);

Upvotes: 0

Kiranramchandran
Kiranramchandran

Reputation: 2094

try this

var lin = document.getElementById("toleft");
var w_controller = lin.getAttribute("w_controller");
alert(w_controller);

DEMO HERE

Upvotes: 4

Related Questions