breezy
breezy

Reputation: 1918

How to change text if url is

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.

Upvotes: 2

Views: 1572

Answers (2)

Robert
Robert

Reputation: 397

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>

Upvotes: 2

hunter
hunter

Reputation: 63522

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

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

Upvotes: 1

Related Questions