wjervis
wjervis

Reputation: 15

How to edit text for a link that does not have an ID using Javascript

I want to change the text of a link using javascript. The problem is this particular link does not have an id. I am unable to change the html, as this is a SharePoint page, and this particular link is created by a page layout, which I do not have access to. Using IE Developer Tools, I see that the HTML surrounding the link is this:

<span id="DeltaPlaceHolderPageTitleInTitleArea">                   
<span>
    <a href="#ctl00_PlaceHolderPageTitleInTitleArea_ctl00_SkipLink">
        <img width="0" height="0" style="border-width: 0px;" alt="Skip Navigation Links" src="" /></a>
    <span>
        <a title="State-Compliance" href="/sites/tax/Compliance/SitePages/State-Compliance.aspx">State-Compliance</a>
    </span>
    <a id="ctl00_PlaceHolderPageTitleInTitleArea_ctl00_SkipLink"></a>
</span>

The link I wish to change is the second one, the one with "State-Compliance" for the tooltip. I looked at jQuery, and found I could use $('#DeltaPlaceHolderPageTitleInTitleArea').find("a").text("Test"); to change the text, but it changes the text of all three links. How can I change just the one? Do I need to iterate through the three, or is there an easier way of getting the link I wish to change?
Sorry if this is a stupid question, I'm a c# developer, and this is my first experience using javascript.

Let me know if you need more information.

Warren

Upvotes: 0

Views: 662

Answers (2)

Jason P
Jason P

Reputation: 27022

How about this, using the attribute equals selector:

$('#DeltaPlaceHolderPageTitleInTitleArea a[title="State-Compliance"]')

Upvotes: 1

j08691
j08691

Reputation: 208040

Use .eq():

$('#DeltaPlaceHolderPageTitleInTitleArea').find("a").eq(1).text("Test");

jsFiddle example

Upvotes: 1

Related Questions