Insert dynamic content into data-text attribute?


Is it possible to generate dynamic created content into the [data-text] attribute on a tweet?

I have a page on my site where users can change different dropdowns create a calculation. I want them to be able to say I added x (substance 1) + y(substance 2) and got z (result) … Is that possible? It’s essentially adding 3 variables and creating 1 javascript string. I’m just not sure if it’s possible to add that into the data-text string.



I had this problem also and - possibly a wee bit of a hack - having faffed a bit without success along the lines of:

(’#tweet_html_id’).attr(“data-text”, newDataText);

Managed to get dynamic text via:


Basically replacing the entire script in the HTML document. Likely not the most elegant way of doing it but… y’know; onto the next bug/“feature”…


Hello there
Can you please elaborate how you acomplish that ?
I Want to change the value of the data-text from the value a div on the page or a javascript variable …

I will apreciate any help
Thanks in advance


I’m not quite sure I understand what you’re asking… what do you want included in the dynamic text? The above code would handle a javascript variable if I remember rightly!


Thanks for answering m8 …
My goal is to read the value of a page element ( a div), and then send it as data-text to a tweet button
I try various methods from arround the web with no succes !!!


The limitation you guys are running into here is that at present, after a Tweet Button has been created, you cannot change the value of text.

There are two ways to proceed, I think:

  1. Dynamically generate a Tweet button once the source text is ready, using twttr.widgets.createShareButton(, , , { text: dynamicText });

  2. Forgo the Tweet button altogether and directly reference the Web Intent, with a click handler to update the ‘text’ parameter in the URL. Something to the effect of this: Tweet, and then:

document.getElementById(‘custom-tweet-button’).addEventListener(‘click’, function (el) { += “?text=” + urlencode(myText);

Hope that helps.


figured this out:

Had to pass the selected value into a stable variable that would exist before the script created the share button.

–end edit

Could you possibly elaborate on the first step listed (“Dynamically generate a Tweet…”)?

I’m trying to do this, using a user selection (cursor highlight) to populate the text parameter:

I’ve also tried the same method, passing the function directly into the param:

In both cases the tweet is created on mouseup, but the selected text isn’t passed.