lolrj11
lolrj11

Reputation: 33

How to get TAG value/text (like innerHTML)?

How to get TAG value (not innerHTML)? Lets supose i have a... link or image.

<link href="bla"> | <img src="ble" />

let suppose there is only 1 link in a document:

var links = document.link;

how to get TAG text as: '<link href="bla">' by JS (cause innerHTML is empty cause there is not text INSIDE tag, cause it does not have ending >)?

Upvotes: 0

Views: 65

Answers (3)

Linoy
Linoy

Reputation: 1395

Not exactly clear the question, but I expect you want the attribute value

var href = $(link).attr('href');

Upvotes: 0

Jack Bashford
Jack Bashford

Reputation: 44145

All you need to do is this (pure JS):

var link = document.getElementsByTagName("link")[0];

Upvotes: 0

heyitsvajid
heyitsvajid

Reputation: 1121

You can use the DOM element attribute outerHTML

$(selector)[0].outerHTML

which gets the first DOM element of the selection and then acquires the html using the DOM attribute outerHTML

If you do not want the content but only the enclosing tag you could do this

$.fn.tag = function(){
    return this[0].outerHTML.replace(this.html(),"");
};

Upvotes: 1

Related Questions