0

I've got two code samples, the first one is working properly, the second one isn't. Both of them should increase a counter when I click on the #incrementBtn.

Here are the samples (and I will provide all the code for further clarifications):

1

$('body').on('click','#incrementBtn', function(){
  let textAreaValue = parseInt($('textarea').val());
  $('textarea').val(textAreaValue+1)
});

2

$('#incrementBtn').on('click', function(){
  let textAreaValue = parseInt($('textarea').val()); 
  $('textarea').val(textAreaValue+1)
});

And one more question, what is the difference between using this (code doesn't work):

let textArea = $(<'textarea'>);      

let textAreaValue = parseInt(textArea.val());
textArea.val(textAreaValue+1);

And this (code works):

let textAreaValue = parseInt($('textarea').val());
$('textarea').val(textAreaValue+1)

Here is the full working code: https://jsfiddle.net/wjnjdk72/3/

1
  • why you write $(<'textarea'>); with '<' '>' , remove that and write $('textarea'); Commented Mar 29, 2018 at 9:54

2 Answers 2

1

First issue:

You're applying the onclick handler to the element before you've inserted it dynamically into the DOM. The body handler works because it's listening for click events on the body element (which bubble up to it) from an element that matches that #incrementBtn selector (it doesn't matter if it's added to the page later on).

You can see this in action by swapping the order, if you add the element first it works:

incrementCounter('#wrapper'); // add the element first

$('#incrementBtn').on('click', function(){
  let textAreaValue = parseInt($('textarea').val()); 
  $('textarea').val(textAreaValue+1)
});

Second issue:

This syntax is invalid Javascript: $(<'textarea'>); You've added some random <> there.

Sign up to request clarification or add additional context in comments.

2 Comments

I should have been more specific with the code that I gave, in the full code I used "let textArea = $('<textarea>'); " to define an html element and then append it to a div element. So my question boils down to this on this issue: Should I use textArea as a way to access it in the on.click function or should I just use 'textarea'?
<textarea> is an invalid selector, it would always be textarea, ideally you would uses classes or IDs as using tag names will cause unexpected things to happen if you have other elements of the same tag on the page. Note that IDs are rarely considered good practice as people like to make reusable components and they can only appear once, or potentially have ID clashes with other elements and have unexpected behaviour
0

With jQuery you select HTML tags only with its name: "textarea", you don't need to include the <> characters

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.