मान लीजिए हमारे पास URL के साथ निम्नलिखित एंकर टैग है:
<a class="demo" title="get the url" href="./mainPage.jsp/1245">href value at console</a>
हमें केवल URL वैल्यू चाहिए यानी href एट्रिब्यूट की वैल्यू। इसके लिए, attr() का उपयोग करें:
attr('href')
उदाहरण
निम्नलिखित कोड है:
<!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>
उपरोक्त प्रोग्राम को चलाने के लिए, फ़ाइल का नाम "anyName.html(index.html)" सेव करें। फ़ाइल पर राइट-क्लिक करें और VSCode एडिटर में "Open with Live Server" विकल्प चुनें।
आउटपुट
यह कंसोल पर निम्नलिखित आउटपुट देगा:

कंसोल में आउटपुट:
