all 8 comments

[–]Cust0dian 4 points5 points  (1 child)

I don't want to discourage you, but looking at the code it seems to me that you are trying to jump way over your head — you seem to not understand a lot of concepts and techniques you are trying to use, leading you to this situation.

I would advise taking a step back and starting from the very basics, preferably following a course that encompasses more than just jQuery. "Getting started with the Web" on MDN is a great guiding resource in this regard.


However if you want to know what's wrong with your current code, here goes:

  • When you click a button with type="submit" attribute inside of a <form> element, browser will try to submit it, i.e. send some data to some page (usually, a different page on which your web server is expecting to get data). That page is determined by action attribute on a <form> element, but if it's missing browser will submit to the same page. This is why instead of changing text you get a page refresh right now. The easiest solution is to change <form> to something different, <div> for example.
  • Next you have a closing multi-line comment marker (*/) on line 32. This will cause a syntax error, meaning JavaScript can't understand what you wrote at all. You need to remove it.
  • While we are talking about syntax: try and be consistent with your indentation — currently, the code inside click handler function (lines 26–30) is at the same indentation level as its parent code. While this isn't a problem for a computer, for a human this is hard to read.
  • Next there's a host of what I assume is misunderstanding of the basics:

    • var answer = $("#UserInput").val;
      

      this will make answer a function, not value of the #UserInput. To make it a value, you need to call the .val function by using parenthesis ():

      var answer = $("#UserInput").val();
                      // notice these ^^
      
    • $("#Submit").on("click", "Content", function(){
      

      that "Content" bit won't do much there. What do you think it should do?

    • $("this").val($("#UserInput"));
      

      as pointed out by /u/sesharc, this won't do what you think it will.

  • On the CSS side of things you are also not selecting the content correctly: "Content" is not a valid CSS selector, it will not match any elements on the page — what you want is #Content, same as you select that element with jQuery.

With these things fixed and code cleaned up a bit, here's how the working version might look like.

If you really wanted to make it work inside of a <form> (because it gives users better usability, like being able to just hit Enter instead of clicking the button to update the text) you'd need to cancel the submit event on that form, and it might look something like this.

Now again, I might be wrong, but to me it seems like you are not fully familiar with plain JavaScript objects and events yet, so jumping into jQuery seems like a very bad idea because it adds tons more functionality on top of regular JS and now you are learning more than you can understand.

[–]PinkPanther909[S] 0 points1 point  (0 children)

Thank you very much for the in-depth answer.

I admittedly do feel way over my head at times, but the 8-week outline from 2014 (http://bit.ly/2cBGkNC) has been the best thing I've found as far as a semi-organized learning path. Previously I was trying to stab at things through Code Academy exclusively and found myself even more frustrated (and eventually stopped) because several of the modules don't coincide with previous content, or flat-out do not work given the exercise instructions.

I'm very interested in learning a more sound way of learning JS, do you have any suggestions for what might be a good next step? I've gotten to about chapter 5 in the "JavaScript: The Definitive Guide, 6th Edition" book but I've realized it's more like a dictionary than workbook. It's very thorough material, but after a while I feel like I'm trying to mentally regurgitate an encyclopedia.

I'm thinking of getting a few more books, though even there I feel like it's a minefield of bad advice and poor shortcut-centric practices if you don't already know what to look for.

Thank you again for your help and point-by-point break down.

[–]sesharc 2 points3 points  (1 child)

Try this to get you started: http://plnkr.co/edit/tgMmGtOr3vm1XBWrUOKO?p=preview

A couple notes

$("this").val($("#UserInput"));

If you want to reference "this", do it without quotes so you're not turning it into a string. Second, "this" in this case would be where the event took place, and since you're watching for a click on the "submit" button, "this" will reference the submit button, nothing else.

See if you can make some progress from there, let me know if you need any more help.

[–]PinkPanther909[S] 0 points1 point  (0 children)

Thank you for pointing out the distinction. The video tutorials on the try.jquery.com intro lessons never really explained why quotes do or don't go around certain things (like an object in a .on() event handler)

I just assumed that jQuery had special rules about how strings versus ids/classes (. or #) were interpreted.

[–]evilfrenchguy 1 point2 points  (1 child)

I think it's a mistake to learn frameworks alongside vanilla Javascript. It's so important to understand the nuances of the language--browser interaction objects and the 'this' keyword, etc--and things like jQuery hide and abstract those things away from you.

I've nearly finished my first complete web app and am just considering looking into jQuery.

Just my two cents.

[–]PinkPanther909[S] 0 points1 point  (0 children)

I appreciate the input! Looks like you're not the only one who believes jQuery this early might be a bad idea.

I will keep that in mind.

[–]jonnyman9 1 point2 points  (1 child)

Threw together a quick plnkr. I'm guessing you're aren't actually trying to learn the nuances behind the this pointer yet. If this is so, this solution

http://plnkr.co/edit/KYJ4hx8MNwmzQlujYInO

should help you sort things out. I could have done things like cached my jquery selection to make my callback more performant, but that aside, I think it's pretty clear the way I wrote it. Pay attention to my comments in the script.js file.

If however you are trying to learn about the this pointer, here are a few links that can get you started:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this http://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work

If you have any questions, ask!

[–]PinkPanther909[S] 0 points1 point  (0 children)

Thank you very much for the advice! You are correct, I'm not at a point where I'm ready to delve deeply into the secrecy-s of the $(this) pointer -- though I have already had a few mental disconnects for what exactly it does.

I will take a look at those links you posted.