I managed to get help on another thread for changing the title and link of an element using JavaScript (Issue with changing the title of a link using jQuery).
I have tried implementing the technique there to another element on my website but am instead met with an error Error 404: The page your are looking for cannot be found.
Thus, while this code works in isolation:
var href = jQuery('#_tab_1 > div > div > div:nth-child(3) > div > div.pf-body > ul > li:nth-last-child(1) > p.item-property').html();
var link = "<a href='"+href+"' target='_blank'>Click Here</a>";
jQuery('#_tab_1 > div > div > div:nth-child(3) > div > div.pf-body > ul > li:nth-last-child(1) > p.item-property').replaceWith(link);
The following code leads to the error mentioned above.
var href = jQuery('#_tab_1 > div > div > div:nth-child(3) > div > div.pf-body > ul > li:nth-last-child(1) > p.item-property').html();
var link = "<a href='"+href+"' target='_blank'>Click Here</a>";
jQuery('#_tab_1 > div > div > div:nth-child(3) > div > div.pf-body > ul > li:nth-last-child(1) > p.item-property').replaceWith(link);
var href2 = jQuery('#c27-single-listing > section > div.profile-cover-content.reveal.reveal_visible > div > div > ul > li:nth-child(3) > div').html();
var link2 = "<a href='"+href2+"' target='_blank'>Click Here</a>";
jQuery('#c27-single-listing > section > div.profile-cover-content.reveal.reveal_visible > div > div > ul > li:nth-child(3) > div').replaceWith(link2);
Attached is the link to the html code I am targeting (Link). Note that the link only reflects the html related to the javascript code that I can't get working ie. href2
. I had not posted the segment that href was targeting as it might get too messy. My purpose is to replace www.hotmail.com
with the words Click Here
, but the user should be able to select Click Here
and it would direct them to www.hotmail.com
. Also, because I had to select only the relevant portions of the html, the selector you view in the link would be different from what is stated in my code above.
Thank you for your help.
Use
:eq()
Selector to select the 4thli
and then replace its content:HTML:
JQuery:
Working Demo here
Here's a snippet that does what you want. Notice that you can use either a class (for multiple elements) or an id (for a singular element).
Basically, I used several commands to do what you wanted:
each()
- should do the following function for each element found by the selector.this
will refer to the DOM elementtext()
- return the text (without html) for that itemreplaceWith()
- will replace the element with whatever you wantin this example, every item with the class
should-become-link
will become a link after 2 seconds (because of thesetTimeout
). The original text inside the element will be used as the newhref
value.and here's a codepen