Need help with a Tweet Button

Need help with a Tweet Button
0.0 0

#1

Hello! I cant understand how to make a tweet button work correctly. Ive installed it using the Tweet Developer Documentation. And Im trying to set a 'date-text' attribute by JQuery, but it doesnt work. Would you please review my CodePen and help me to find out what am I doing wrong?


#2

I’m not really sure how their widget works, but you can construct a URL to set your href to with the quote and author.

For example a tweet for hello world could be constructed with the URL below:
var url = https://twitter.com/intent/tweet?hashtags=hashtag&text=hello%20world

Note that spaces in the text should be replaced with %20 in urls

To set the href attribute, you can use $('.twitter-share-button').attr('href', url)


#3

Thank you! :sunny:
Ive seen solution like this, with the url. But if I cant set the ‘text’ attribute, how can I change ‘href’ attribute?(( For some unknown reason I can`t control the ‘.twitter-share-button’ element by JQuery :pensive:


#4

I`ve decided to give up and make the button without the Tweet Button widget X))


#5

The <a> tag doesn’t have a text attribute, so that is why you are unable to control it using .attr(). For reference, the attributes for <a> are listed here: https://www.w3schools.com/tags/tag_a.asp

If you define a tweet-url variable, a line like this should work to set the href attribute:

 $('.twitter-share-button').attr('href', tweet-url)

#6

I mean ‘data-text’ attribute, that supposed to be, and I could set it in html, but not by jquery. But I`ve deleted it anyway, so no matter)


#7

If you want to change the value of an attribute called ‘data-text’ for an element with class="somelass) with jQuery, you could write:

$(.someClass).data("text", "myValue");