0

just want to ask if how to add a variable in the href? for example this is the my javascript:

    <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
    $( document ).ready(function() {
        function setLinkValue(value) {
            var numItems = $('[id^=site-]').length;

            for (i = 0; i < numItems; i++) { 
                var link = document.getElementsByClassName('site-link');
                link.href = link.href.replace('putNatsvarHere', value);
            }

        }

        //You would pass netsvar here
        setLinkValue('ExampleValue');

    });
</script>

I want to insert it into my a tag in href

<a href="http://websitelink.com/track/putNatsvarHere/pt?thumb_id=302&gn=pt&gi=01.jpg" id="site-link" class="title" target="_self">Exclusive February Sale!</a><br>

    <a href="http://websitelink.com/track/putNatsvarHere/pt?thumb_id=302&gn=pt&gi=01.jpg" id="site-link" class="title-link" target="_self">Sale!</a>
    <br>
    <a href="http://websitelink.com/track/putNatsvarHere/pt?thumb_id=302&gn=pt&gi=01.jpg" id="site-link" class="title-link pink-play" target="_self"> February Sale!</a>
2
  • you can use onClick attribute and call getCookie method Commented Sep 18, 2015 at 16:55
  • How i gonna use that? can you give me a sample code? Commented Sep 18, 2015 at 16:58

1 Answer 1

1

You can just replace a segment in the URL with the value, and you don't even need jQuery to do it. See the working example below.

function setLinkValue(value) {
  var link = document.getElementById('site-link');
  
  link.href = link.href.replace('putNatsvarHere', value);
}

//You would pass netsvar here
setLinkValue('ExampleValue');
<a href="http://websitelink.com/track/putNatsvarHere/pt?thumb_id=302&gn=pt&gi=01.jpg" id="site-link" class="title-link pink-play" target="_self">Exclusive February Sale!</a>

You could call setLinkValue in this way:

var natsvar = getCookie("nats");
setLinkValue(natsvar);

To update multiple elements, you could use getElementsByClassName and loop through them.

function setLinkValue(link, value) {
  link.href = link.href.replace('putNatsvarHere', value);
}

var elements = document.getElementsByClassName('title-link');
var i = 0;

for (i = 0; i < elements.length; i++) {
  //You would pass netsvar here
  setLinkValue(elements[i], 'ExampleValue');
}
<a href="http://websitelink.com/track/putNatsvarHere/pt?thumb_id=302&gn=pt&gi=01.jpg" class="title-link pink-play" target="_self">Exclusive February Sale!</a>
<a href="http://example.com/putNatsvarHere/" class="title-link pink-play" target="_self">Another link</a>

Sign up to request clarification or add additional context in comments.

10 Comments

It didnt work for me. but i think this is the way i wanted to happen
the link on my send became link this <a href="websitelink.com/track/putNatsvarHere/…" id="site-link" class="title-link pink-play" target="_self">Exclusive February Sale!</a> instead the value of putNatsvarHere
can you help me @drew?
@SheerwoodJohnCaday I'm not sure what you're asking. If you run the snippet in the answer, you can see that putNatsvarHere in the link becomes ExampleValue, which in your case would be the value of the cookie instead.
I think it was only applied in one link? i tried but it look only one link has changed.
|

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.