2

I tried searching for this question but couldn't find an answer.

My intention is to change the text that is wrapping the href link. For example

I have this snippet of code.

    <div class="change-this">
        <a href="http://example.com/login.php">Sign in</a> or 
        <a href="http://example.com/login.php?action=create_account" >Create an account</a>
    </div>

I want to change 'Create an Account' to 'New user' and want to change 'Sign In' to 'Log In'. How can i achieve this by grabbing the URL and changing the text based on the URL within this div.

I was thinking of something like this.. But this doesn't work.

 $('.change-this a').text(function(i, oldText) {
    return oldText === 'Create an Account' ? 'New User' : oldText;
    console.log('done');

The reason why i have to do it this way is because the text are variables within the CMS and I can't change this but my work around solution was to do it with jQuery or Javascript. Just don't know how to implement this. If anyone can help that would be great. Thanks.

6
  • so when do you want to change it? when the page gets loaded, or when you click somewhere (where do you click to change it)? Commented Dec 10, 2014 at 21:08
  • sorry forgot to include that @Barmar Commented Dec 10, 2014 at 21:08
  • yes on page load. @caramba Commented Dec 10, 2014 at 21:08
  • Your code should work. Is it inside $(document).ready()? Commented Dec 10, 2014 at 21:09
  • 2
    Create an Account but Create an account. This is the only problem. Commented Dec 10, 2014 at 21:10

2 Answers 2

2

How about something like this.

<html>
<head>
    <script type="text/javascript" src="jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
        $(function () {
            var txt = $('div.change-this a');
            txt.first().text('Log In');
            txt.next().text('New User');
        });
    </script>
</head>
<body>
    <div class="change-this">
        <a href="http://example.com/login.php">Sign in</a> or 
        <a href="http://example.com/login.php?action=create_account" >Create an account</a>
    </div>
</body>
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

This worked!! thanks @robert i should have asked if someone knew of a better way of doing this! thanks man!
@BeEasy looks like Robert helped you with this answer. You should mark it as the right answer, so it's kind a closed question.. (maybe even upvote it)
1

Could use the attribute contains selector based on the href url?

$(".change-this a[href*='action=create_account']").text("New User");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.