How to append the id of the anchor tag to the new href in jquery and replace the href attribute in the page?


Notary class

I have the following:

<a id="sample"></a>

I want to change it to:

<a href="https://www.exampledomain/testpage.aspx#sample"></a>

Use jQuery or pure JavaScript. "Sample" is a non-unique dynamic property, but I need the "pre-added" field to always be the same value. Thank you in advance.

zer00ne

try .querySelectorAll()and .forEach(). This demo adds href, text and title( the full value is displayed hrefwhen the user hovers over the link )

var linx = document.querySelectorAll('a');

linx.forEach(lnk => {
  let ID = lnk.id;
  const host = 'https://example.com/#';
  lnk.href = host + ID;
  lnk.title = host + ID;
  lnk.textContent = ID;
});
a {
  display: block
}
<a id="ID0"></a>
<a id="ID1"></a>
<a id="ID2"></a>
<a id="ID3"></a>
<a id="ID4"></a>
<a id="ID5"></a>
<a id="ID6"></a>
<a id="ID7"></a>
<a id="ID8"></a>
<a id="ID9"></a>
<a id="IDA"></a>
<a id="IDB"></a>
<a id="IDC"></a>
<a id="IDD"></a>
<a id="IDE"></a>
<a id="IDF"></a>

Related


How to replace some text in href of anchor tag in jQuery

Ram Singh I have few url anchors like "http%3a/test.dev.test.com/posts/test test" and I want to replace it with " http://test.dev.test.com/posts/test Test" on the window loading function, I have tried the following code but it does not update the url of the an

How to replace some text in href of anchor tag in jQuery

Ram Singh I have few url anchors like "http%3a/test.dev.test.com/posts/test test" and I want to replace it with " http://test.dev.test.com/posts/test Test" on the window loading function, I have tried the following code but it does not update the url of the an

How to replace some text in href of anchor tag in jQuery

Ram Singh I have few url anchors like "http%3a/test.dev.test.com/posts/test test" and I want to replace it with " http://test.dev.test.com/posts/test Test" on the window loading function, I have tried the following code, but it doesn't update the anchor tag's

How to replace some text in href of anchor tag in jQuery

Ram Singh I have few url anchors like "http%3a/test.dev.test.com/posts/test test" and I want to replace it with " http://test.dev.test.com/posts/test Test" on the window loading function, I have tried the following code, but it doesn't update the anchor tag's

Append parent element attribute to URL of anchor tag href

Sahil Sharma <asp:Repeater runat="server" ID="rptStockListings"> <ItemTemplate> <li rank=<%#GetRank(DataBinder.Eval(Container.DataItem,"r")) ............... <a href="<%# DataBinder.Eval(Container.DataItem,"Url") I already have the URL, bu

Append parent element attribute to URL of anchor tag href

Sahil Sharma <asp:Repeater runat="server" ID="rptStockListings"> <ItemTemplate> <li rank=<%#GetRank(DataBinder.Eval(Container.DataItem,"r")) ............... <a href="<%# DataBinder.Eval(Container.DataItem,"Url") I already have the URL, bu

Append parent element attribute to URL of anchor tag href

Sahil Sharma <asp:Repeater runat="server" ID="rptStockListings"> <ItemTemplate> <li rank=<%#GetRank(DataBinder.Eval(Container.DataItem,"r")) ............... <a href="<%# DataBinder.Eval(Container.DataItem,"Url") I already have the URL, bu

How to make anchor tag's href depend on class attribute?

posthumous pacifism I've created a page in an ASP.NET CORE Razor WebApp for a customer table and I'm trying to implement download links for images and files in a database. The client class has a string property "image" that holds the image URL. Since the datab

Append input string to anchor tag href

soft code Trying to append user input string to anchor tag href using vanilla js. <script> function searchProduct(){ var inputId = document.getElementById('inputId').value; document.getElementById('go').href = "mywebsite/product/" + inputId; }

Append input string to anchor tag href

soft code Trying to append user input string to anchor tag href using vanilla js. <script> function searchProduct(){ var inputId = document.getElementById('inputId').value; document.getElementById('go').href = "mywebsite/product/" + inputId; }

Is it possible to use <a> (anchor tag) without href attribute?

Zachary I've been building websites with Twitter Bootstrap, and many of its features rely on wrapping content in <a>, even if they're just executing Javascript. I'm having trouble with the strategy href="#"suggested by the Bootstrap documentation , so I'm tryi

Safari anchor tag with href attribute not getting focus?

men I had this problem, and I thought my code was wrong, but after googling around and looking at related examples, I wonder if Safari really doesn't focus tabs <a>on properties href? In my use case I want <a href="#top">jump</a>to use to jump to page section.

Is it possible to use <a> (anchor tag) without href attribute?

Zachary I've been building websites with Twitter Bootstrap, and many of its features rely on wrapping content in <a>, even if they're just executing Javascript. I'm having trouble with the strategy href="#"suggested by the Bootstrap documentation , so I'm tryi

Safari anchor tag with href attribute not getting focus?

men I had this problem, and I thought my code was wrong, but after googling around and looking at related examples, I wonder if Safari really doesn't focus tabs <a>on properties href? In my use case I want <a href="#top">jump</a>to use to jump to page section.

Safari anchor tag with href attribute not getting focus?

men I had this problem, and I thought my code was wrong, but after googling around and looking at related examples, I wonder if Safari really doesn't focus tabs <a>on properties href? In my use case I want <a href="#top">jump</a>to use to jump to page section.

Safari anchor tag with href attribute not getting focus?

men I had this problem, and I thought my code was wrong, but after googling around and looking at related examples, I wonder if Safari really doesn't focus tabs <a>on properties href? In my use case I want <a href="#top">jump</a>to use to jump to page section.

Safari anchor tag with href attribute not getting focus?

men I had this problem, and I thought my code was wrong, but after googling around and looking at related examples, I wonder if Safari really doesn't focus tabs <a>on properties href? In my use case I want <a href="#top">jump</a>to use to jump to page section.

Safari anchor tag with href attribute not getting focus?

men I had this problem, and I thought my code was wrong, but after googling around and looking at related examples, I wonder if Safari really doesn't focus tabs <a>on properties href? In my use case I want <a href="#top">jump</a>to use to jump to page section.

Is it possible to use <a> (anchor tag) without href attribute?

Zachary I've been building websites with Twitter Bootstrap, and many of its features rely on wrapping content in <a>, even if they're just executing Javascript. I'm having trouble with the strategy href="#"suggested by the Bootstrap documentation , so I'm tryi

Safari anchor tag with href attribute not getting focus?

men I had this problem, and I thought my code was wrong, but after googling around and looking at related examples, I wonder if Safari really doesn't focus tabs <a>on properties href? In my use case I want <a href="#top">jump</a>to use to jump to page section.

Safari anchor tag with href attribute not getting focus?

men I had this problem, and I thought my code was wrong, but after googling around and looking at related examples, I wonder if Safari really doesn't focus tabs <a>on properties href? In my use case I want <a href="#top">jump</a>to use to jump to page section.

Safari anchor tag with href attribute not getting focus?

men I had this problem, and I thought my code was wrong, but after googling around and looking at related examples, I wonder if Safari really doesn't focus tabs <a>on properties href? In my use case I want <a href="#top">jump</a>to use to jump to page section.

Safari anchor tag with href attribute not getting focus?

men I had this problem, and I thought my code was wrong, but after googling around and looking at related examples, I wonder if Safari really doesn't focus tabs <a>on properties href? In my use case I want <a href="#top">jump</a>to use to jump to page section.

Safari anchor tag with href attribute not getting focus?

men I had this problem, and I thought my code was wrong, but after googling around and looking at related examples, I wonder if Safari really doesn't focus tabs <a>on properties href? In my use case I want <a href="#top">jump</a>to use to jump to page section.