0

This question is not a duplicate of Get meta data attribute in javascript. I'd like to get meta name by language.

I have the following in HTML code:

<meta name="DC.Subject" xml:lang="pl" content="wróg" />
<meta name="DC.Subject" xml:lang="en" content="enemy" />

There are also other meta name tags in HTML.

It is quite easy to get the content of the first DC.Subjectby using:

document.getElementsByName("DC.Description")[0].getAttribute("content"))

and the second by

document.getElementsByName("DC.Description")[1].getAttribute("content"))

But this is not language specific. However, how do I get the content of DC.Description by using xml:lang= in JavaScript?

Community
  • 1
  • 1
menteith
  • 596
  • 14
  • 51
  • Take a look at this question, I think it's the same as the one you're asking - http://stackoverflow.com/questions/949341/how-to-obtain-lang-attribute-in-html-using-javascript – Rose Robertson Oct 30 '16 at 18:25
  • 1
    Possible duplicate of [Get meta data attribute in javascript](http://stackoverflow.com/questions/29124579/get-meta-data-attribute-in-javascript) – Asons Oct 30 '16 at 18:29
  • @LGSon Your post does not explain how to get attribute by *language*. – menteith Oct 30 '16 at 18:56
  • I see that now .. was too quick ... but this one does: http://stackoverflow.com/questions/23034283/is-it-possible-to-use-htmls-queryselector-to-select-by-xlink-attribute-in-an – Asons Oct 30 '16 at 18:57
  • @RoseRobertson I do not want to obtain the declared language of the document. – menteith Oct 30 '16 at 18:58

2 Answers2

1

You may use document.querySelector as follows:

var plMeta = document.querySelector('[xml\\:lang="pl"][name="DC.Subject"]');

console.log(plMeta);
<meta name="DC.Subject" xml:lang="pl" content="wróg">
<meta name="DC.Subject" xml:lang="en" content="enemy">

Here's a related Q&A to get more in touch with CSS selectors involving namespaces: Is it possible to use HTML's .querySelector() to select by xlink attribute in an SVG?

Community
  • 1
  • 1
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • It's a dupe so please close it as such – Asons Oct 30 '16 at 18:30
  • @LGSon I believe OP's issue isn't getting a `` but getting it by `xml:lang` which involves escaping `:` – Matías Fidemraizer Oct 30 '16 at 18:31
  • `Error: { "message": "SyntaxError: missing ; before statement", "filename": "http://stacksnippets.net/js", "lineno": 14, "colno": 64 }` – connexo Oct 30 '16 at 18:35
  • @connexo I hope you didn't downvote my answer because of an extra parenthesis :D – Matías Fidemraizer Oct 30 '16 at 18:40
  • @MatíasFidemraizer You are right. I do not want just to get `` (my post shows I can do it). I'd like to know how to get it by `xml:lang` However, in the webpage there are more than one `meta` with `xml:lang`. How do I get ` – menteith Oct 30 '16 at 18:49
  • @menteith You combine the attributes: `document.querySelector('[name="DC.Subject"][xml\\:lang="pl"]');` – Asons Oct 30 '16 at 19:01
  • @MatíasFidemraizer Yes, the linked one would work well to close this question as a dupe ... better than the one suggested as I were a little too quick :) – Asons Oct 30 '16 at 19:02
  • @menteith I've updated my answer to meet this requirement too. – Matías Fidemraizer Oct 30 '16 at 19:03
  • @menteith Can there be more than one element you want to catch? – Asons Oct 30 '16 at 19:11
  • @LGSon There could be several `meta name="DC.Subject"` and also several `xml:lang="pl"` and other `meta name=` if this is what you had in mind. – menteith Oct 30 '16 at 19:14
  • @menteith Well then you will need `document.querySelectorAll` instead, and loop through the array of object – Asons Oct 30 '16 at 19:19
  • Which is what my answer does. – connexo Oct 30 '16 at 19:20
  • @menteith I believe that you've enough info to solve the rest of your requirements yourself :) it's all about combining CSS selectors. It would be a bad idea to turn answer into the CSS selector bible :D – Matías Fidemraizer Oct 30 '16 at 19:20
0

You need to escape the problematic : character in the attribute name:

var xml_lang_elements = document.querySelectorAll('[xml\\:lang][name="DC.Subject"]');
for (var i = 0; i < xml_lang_elements.length; i++) { 
  console.log(xml_lang_elements[i].getAttribute('content'));
}
<meta name="DC.Subject" xml:lang="pl" content="wróg" />
<meta name="DC.Subject" xml:lang="en" content="enemy" />
connexo
  • 53,704
  • 14
  • 91
  • 128