0

I've got a variable and I want to display the value of it in a specific place of my HTML. Much like +variable+ within javascript.

My setup is as followed:

The HTML: short version:

addthis:url="http://example.com/script.php?code="

the HTML full version:

<div class="addthis_toolbox addthis_default_style "
    addthis:url="http://example.com/script.php?code=" 
    addthis:title="An Example Title"
    addthis:description="An Example Description">

    <a href="http://www.addthis.com/bookmark.php?v=250&pubid={YOUR_PROFILE_ID}" class="addthis_button_compact">Share</a> 
      <span class="addthis_separator">|</span>
      <a class="addthis_button_preferred_1"></a>
      <a class="addthis_button_preferred_2"></a>
      <a class="addthis_button_preferred_3"></a>
      <a class="addthis_button_preferred_4"></a>
    </div>

I would like to "print" the value of my variable after the = so it would result in:

addthis:url="http://example.com/script.php?code=myVar"

I had found document.write but this won't work since I will have to place the script tags between quotes.

Hope someone can help me out!

1

3 Answers 3

3

String concatenation

addthis:url="http://example.com/script.php?code=" + myVar;
Sign up to request clarification or add additional context in comments.

1 Comment

Maybe I understand incorrectly but I think I've been incomplete in my question. I can't insert script tags after the quotes either because the HTML tag hasn't been closed yet. I will make an edit with all of the HTML for clarification. Thanks for your time though!
1

Try to use a placeholder and replace it with jquery:

https://stackoverflow.com/a/7480394/1380486

Comments

1

You wont be able to do it the way you want (with document.write). Even if you solve the quote problem, you will still have to have script tags inside of your div tags like this:

HTML

<div addthis:url=<script>document.write("http://example.com/script.php?code=myVar")<script>>

This simply will not work.

With jQuery you could select that element and add the attribute when the dom is ready.

JavaScript

$(document).ready(function(){
    $(".addthis_toolbox").attr("addthis:url","http://example.com/script.php?code=myVar")
});

5 Comments

Hi, Thanks for that. I did not know it was possible to add custom attribute's. However I'm still unclear on how the variable gets added in the url? shouldn't it be +myVar+ ?
Yes, in javascript to add a variable to a string you would use "http://"+url+".com"
Also, if you have multiple divs with the addthis_toolbox class, you may need to give them some sort of identifier. $(".addthis_toolbox") will grab every element with the class="addthis_toolbox", therefore this code will set that attribute on every element with that class
Thanks! I'm almost there :) Could you please help me on my last bit? I tried: $(".addthis_toolbox").attr("addthis:url","example.com/script.php?code=" + myVar") but this results on a syntax error.
You just need to remove the final quote at the end of the line $(".addthis_toolbox").attr("addthis:url","example.com/script.php?code=" + myVar") it should be like this $(".addthis_toolbox").attr("addthis:url","example.com/script.php?code=" + myVar)

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.