Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

How to change hyperlink display text without know the ID?

I want to use java script to change the display text of some hyperlinks in a web page.
Because the page is created by Sharepoint, i can no the ID of the href.
Thus, i cannot use document.getElementById to locate the hyperlink.
Any idea how can i change the display text of the hyperlink by the original display text?
For example, if the display text is "7", then change to "07:00" automatically.

Thank you.
0
dickchan
Asked:
dickchan
  • 2
1 Solution
 
Sudaraka WijesingheWeb Application ProgrammerCommented:
I don't think there's a straight forward way to do this using plain JavaScript, However you can use jQuery.

$("a:contains('7')").html('7.00');

Open in new window


http://jsfiddle.net/t6CwM/
http://api.jquery.com/contains-selector/
0
 
leakim971PluritechnicianCommented:
check this : http://jsfiddle.net/JYpqB/

<a href="http://www.google.com">Google</a><br />
<a href="http://www.google.com">Google</a><br />
<a href="http://www.yahoo.com">Google</a><br />
<a href="http://www.google.com">Google</a><br />

Open in new window

document.getElementsByTagName("a")[2].innerHTML = "Yahoo!";

Open in new window

0
 
experts1Commented:
Javascript example below wil loop through all links and
change only those displaying "7" to "07:00" automatically:

<script>
function changeHyperlinks()
<script>
function changeHyperlinks()
{
                var hyper_links = document.getElementsByTagName("a");

                for (var x = 0; x < hyper_links.length; x++)
                {
                       var hLink = hyper_links[x];
                       if (hLink.innerHTML == "7")
                       {
                               hLink.innerHTML = "07:00"
                       }
                }
}
</script>
0
 
experts1Commented:
Sorry, update from previous!

Should be as follows:

<script>
function changeHyperlinks()
{
                var hyper_links = document.getElementsByTagName("a");

                for (var x = 0; x < hyper_links.length; x++)
                {
                       var hLink = hyper_links[x];
                       if (hLink.innerHTML == "7")
                       {
                               hyper_links[x].innerHTML = "07:00"
                       }
                }
}
</script>
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now