Uncaught TypeError: Unable to call "replace" method from undefined

$(this).find("input[name=amount]").val($(this).find("input[name=amount]").val().replace('$', '')); 

Keep getting this error in my developer tools. I just want to replace the $ symbol with nothing that is. ''

Thoughts?

+4
source share
1 answer

Your error says that there is no element that matches your selector, so element.val() returns undefined that does not have a replace method. Try debugging it and console.log() at each step.

In addition, you do not need to search for an item twice. Just save it in a variable:

 var $input = $(this).find('input[name="amount"]'); $input.val($input.val().replace('$', '')); 
+10
source

Source: https://habr.com/ru/post/1391715/


All Articles