

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript - Get href value
Let’s say we have the following anchor tag with URL −
<a class="demo" title="get the url" href="./mainPage.jsp/1245">href value at console</a>
We need to get only the URL value i.e. the href attribute value. For this, use attr() −
attr('href')
Example
Following is the code −
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <body> <ul class="getURLDemo"> <li> <a class="demo" title="get the url" href="./mainPage.jsp/1245">href value at console</a> </li> </ul> </body> <script> var hrefValue = $('ul.getURLDemo li a.demo').attr('href'); console.log(hrefValue); </script> </html>
To run the above program, save the file name “anyName.html(index.html)”. Right click on the file and select the option “Open with Live Server” in VSCode editor −
Output
This will produce the following output on console −
The output in console −
- Related Questions & Answers
- Selenium Click Link By href Value
- How to get an attribute value from a href link in selenium?
- JavaScript function in href vs. onClick
- How to search the value of the href attribute of a link in JavaScript?
- Get key from value in JavaScript
- How to get the hash part of the href attribute of an area in JavaScript?
- How to get the port number part of the href attribute of an area in JavaScript?
- Get value from div with JavaScript resulting undefined?
- How to get the value of PI in JavaScript?
- JavaScript - know the value of GET parameters from URL
- Get max value per key in a JavaScript array
- How to get the content of href within some targeted class using selenium?
- HTML DOM Anchor href Property
- HTML DOM Link href Property
- HTML DOM Location href Property
Advertisements