0

Here is the HTML code:

<a class="tweet">
    <button class="btn btn-default btn-md">

        <i class="fa fa-twitter hidden-sm hidden-md hidden-lg"></i><span class="hidden-xs">Tweet This</span>

    </button>
</a>

And the gist of the JS

function tweet(message, author) {
  window.open('https://twitter.com/intent/tweet?hashtags=thequotemachine&text=' + encodeURIComponent('"' + message + '" ' + author + " via"));
}

and

$('button.tweet').click(function() {
  var currQuote = $('#quote').text();
  var currAuthor = $('#author').text();
  var truncatedString = truncateString(currQuote, currAuthor)
  tweet(truncatedString, currAuthor);
});

When clicking it is supposed to take 'quote', which is a piece of text from the page, and it's supposed to open a new window for the user to 'tweet' that quote. Right now it does nothing when clicked but I can't figure out why. I'm sure I'm missing something very embarrassingly basic. Maybe I just need a second set of eyes on this...

pmath325
  • 61
  • 8
  • 1
    Your jQuery selector is wrong, that's why nothing is happening. Should be just $(".tweet").click(func... Also for the sake of conventions, may want to add a semi colon after your third variable. Hope this helps. – Andrew Garrison Dec 14 '17 at 20:30
  • Learn to use console.log and debugger statements to debug – epascarello Dec 14 '17 at 20:31
  • Thanks @AndrewGarrison that helps a lot. As I thought, it was something simple I was overlooking. – pmath325 Dec 14 '17 at 20:33
  • @epascarello how would you have? – pmath325 Dec 14 '17 at 20:33
  • Add console inside the click to see if it is called.... nope... okay. Add one outside the function to see if the JavaScript is loading....okay worked. ay now lets check to see the element is found `console.log($('button.tweet').length);` .... nope... okay now why is it not finding the element.... – epascarello Dec 14 '17 at 20:41
  • And FYI: button in anchor is invalid html https://stackoverflow.com/questions/6393827/can-i-nest-a-button-element-inside-an-a-using-html5 – epascarello Dec 14 '17 at 20:42

1 Answers1

2

Your selector button.tweet would match a button with the class tweet. From your HTML structure you need .tweet button or just ad .tweet to your button element.

Pat
  • 2,540
  • 1
  • 21
  • 28
  • Ah, so having the 'a' class being 'tweet' was the wrong way to go? – pmath325 Dec 14 '17 at 20:32
  • I would remove the `button` element completely, and just add `btn btn-default btn-md` to the anchor. Then change your selector just be `.tweet` – Pat Dec 14 '17 at 20:38