Raffaele Spataro
Raffaele Spataro

Reputation: 11

Add text to twitter share API via jquery/javascript

I can't manage to make jQuery add the content of #quote (which is a paragraph with a string generated via foresmatic API).The full code is here: https://codepen.io/raffaele2692/pen/GvrvxM .... Can you help me? :)

<a type="button" class="twitter-share-button"
href="https://twitter.com/intent/tweet"
data-size="small"
data-text="">
Tweet</a>


<script>
var textQuote = document.getElementByID("#quote");
$("a").attr("data-text", textQuote);
</script>

Upvotes: 1

Views: 415

Answers (2)

Jimmy
Jimmy

Reputation: 73

I think textQuote is a HtmlElement object, not the value you want to assign. you should get the value in quote first. btw, jquery has a method called data to assign value to data attributes.

Upvotes: 1

Vladu Ionut
Vladu Ionut

Reputation: 8193

You have some issues in the js code. You do not need to add "#" to the id for getElementByID call , also to get the text of a HTML element you can use text method.

<script>
var textQuote = $("#quote").text();
$("a").attr("data-text", textQuote);
</script>

Upvotes: 3

Related Questions