There's a JavaScript-based compliment generator that generates new compliments at the click of a button. Users can continually generate new compliments until they get one they like.
I'd like to add the option to share any generated compliment via Twitter but am not sure how to add the compliment text to the Twitter sharer link. Currently, the compliment text only gets displayed within a div with a specific ID.
The generated compliment is output to the DIV with an id of "compliment" and is working as I require. However, I don't know how to ALSO output the compliment text into the Twitter share link -- especially if the text needs to be formatted differently from how it is displayed within the div).
I've setup two jsfiddles with the progress I've made so far.
Here is my first attempt:
http://jsfiddle.net/LosHantos/g04u3qxa/
Here is the JavaScript from that fiddle:
var Complimenter = primish({
a: 'lovely,beautiful,gorgeous,kind,wonderful,nice,perfect'.split(','),
b: 'good-looking,super-duper,awesome'.split(','),
c: 'pal,friend,person,human'.split(','),
d: 'improver,supporter,juggler'.split(','),
combos: ['a,b,c', 'a,b,d', 'b,c', 'b,d'],
constructor: function(element) {
element && (this.element = element);
this.combos = this.combos.map(function(c) {
return c.split(',')
});
this.compliment();
},
compliment: function(e) {
e && e.preventDefault();
var compliment = this.get();
this.element && (this.element.innerHTML = compliment);
return compliment;
},
get: function() {
var c = this.combos,
self = this;
return c[Math.random() * c.length >> 0].map(function(k) {
return self[k][Math.random() * self[k].length >> 0]
}).join(' ');
}
});
(function() {
var complimenter = new Complimenter(document.getElementById('compliment'));
document.querySelector('.heading').addEventListener('click', complimenter.compliment.bind(complimenter));
}());
I got as far as changing the Twitter share link from the original fiddle, then adding this new var to the JS:
var link = "https://twitter.com/intent/tweet?hashtags=compliment&related=complimenttime&text=" + complimenter;
document.getElementById('tweet').setAttribute("href", link); //assigns url above as href to twitter button
document.getElementById('compliment').innerHTML = "<span class='bold'>" + complimenter + "</span>" + "<br />";
The fiddle for that change is here http://jsfiddle.net/LosHantos/catx520n/ (My second attempt)
But, the problems I'm seeing are:
- It displays the string
[object object]
instead of the compliment - If you click
generate a new compliment
, a compliment then replaces[object object]
but the Twitter sharer still uses[object object]