.replace () on jQuery object

3

Good afternoon everyone.

I must say at the beginning that it is my first question here. I usually just read the questions of others, but I do not find a solution to this problem anywhere on the internet.

I have a code that takes a value from a field in html. I want to use a jQuery object in a jQuery property, but I do not know how to do it.

The point is I want to put a ZIP code mask in place, and the dash ('99999-999') cuts the numerical value of the zip to the first 5 digits. After going far behind, I made the following code to be able to take this stroke out of the move and only play with the zip numbers, but for some reason it does not work. Here's what I did:

var cep = parseInt($('#ip-cep').val.toString().replace(/-/, ''), 10);

Just to clarify, I get the value of the html field, transform it into a string, retreat the trace with .replace (), and then convert it back to numeral to make the comparisons. The point is it does not work. The value of 'cep' always returns as NaN, even with numerical values only. If I took out the trace with replace, when parseInt goes into action there should only be numbers in place to be converted, since the mask only allows the user to type numbers.

What am I doing wrong? Thank you for your attention.

    
asked by anonymous 24.04.2018 / 21:22

2 answers

3

You are using .val erroneously. The correct syntax is .val() :

var cep = parseInt($('#ip-cep').val().toString().replace(/-/, ''), 10);
console.log(cep);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputid="ip-cep" value="71000-123">
    
24.04.2018 / 21:29
0

Opa,     I checked here and tested it with a local variable,     Then I got the value with your own code to test, and it worked! (you can run in the browser to test, just press F12 and go to Console)

var testeCep = '99999-999';
var cep = parseInt(testeCep.toString().replace(/-/, ''), 10);

That is, the problem is in the method in which you take the value, which instead of .val, would be .value (no parentheses), I hope it helped! Here is the code below hugs!

var cep = parseInt($('#ip-cep').value.toString().replace(/-/, ''), 10);
console.log(cep);
    
24.04.2018 / 21:35