Is any way, how to without jQuery get from string value of href attribute in "a" tag?
I have got string with (for example) this content - <a href="www.something">
.
How can I get value of href attribute to variable?
The jQuery attr () method is used to get attribute values. The following example demonstrates how to get the value of the href attribute in a link: The next chapter explains how to set (change) content and attribute values.
Get Attributes - attr() The jQuery attr() method is used to get attribute values. The following example demonstrates how to get the value of the href attribute in a link: $("button").click(function(){. alert($("#w3s").attr("href"));
Getting it via getAttribute returns the actual attribute's value rather than the fully-qualified version, and the href reflected property gets the fully-qualified version (e.g., relative links get expanded). Getting a reference to the element is a broad topic.
Using JavaScript In vanilla JavaScript, you can use the querySelector(), which will return the first anchor tag within the document. Then we can directly access the href property to get the exact value of the href attribute.
I have got string with (for example) this content -
<a href="www.something">
If it is actually in a string as follows:
var s = '<a href="www.something">';
Then you can use a regex with the .match()
method:
var href = s.match(/href="([^"]*)/)[1];
// href is now www.something
If it is an element on your page then have a look at T.J. Crowder's answer (no need for me to duplicate that information here).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With