javascript - Add target attribute to anchor tags without id or class -


I have input which allows users to add text to the hyperlinks anchor tags. I would like to keep it simple for the user so that they all need to enter & lt; A href = "www.google.com" & gt; Link & lt; / A & gt; But target attribute

to organize a tag is a great way in JavaScript, can I do this without the ID associated with the tag? (Like getElementById will not work here). This is known by other entries found in StackVarfarflow, but only when the anchor tag has an ID

These links, however, will always be the same parent div class.

This is a JavaScript-only way to accomplish this:

  Var NLink = $ ("div.notification-popup-message> a"); NLink.click (function (e) {e.preventDefault (); window.open (this.href);});  

Another method that uses the querySelecter that actually attaches to HTML in the DOM:

  var messageLink = document.querySelectorAll ("div .message & gt; A "); $ (Function () {$ (message link) .attr ("target", "_blank");});  

Comments