Reputation: 45
I need to share content of a <textarea>
via twitter. I have a js function quotes(), which generates a random quote and this html:
<textarea id="txtbox" style="width:600px; height: 50px;" ></textarea>
<button onClick="quotes()">Click Here</button>
Below I need a twitter share button, like this:
<a href="https://twitter.com/share" class="twitter-share-button"{count} data-url="goo.gl/udj2qQ" data-text= '' >Tweet</a>
but in the data-text section I need my quote.
Upvotes: 3
Views: 714
Reputation: 29287
The solution is to remove the old button, create a new one and run the tweeter
sdk code again using twttr.widgets.load();
More information:
function quotes() {
var txt = document.querySelector('textarea').value;
var tbutton = document.querySelector('.twitter-share-button');
tbutton.parentNode.removeChild(tbutton);
var newA = document.createElement('a');
newA.setAttribute('href', 'https://twitter.com/share');
newA.setAttribute('class', 'twitter-share-button');
newA.setAttribute('data-url', 'goo.gl/udj2qQ');
newA.setAttribute('data-text', txt);
document.body.appendChild(newA);
twttr.widgets.load();
}
<textarea id="txtbox" style="width:600px; height: 50px;"></textarea><br />
<button onClick="quotes()">Click Here</button>
<hr />
<a href="https://twitter.com/share" class="twitter-share-button"{count} data-url="goo.gl/udj2qQ" data-text="blabla">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
Upvotes: 1