Hugo Silva
Hugo Silva

Reputation: 352

Error on getting value of input text with jQuery and comparing it to another field

i am trying to get the val of a input text that is inside a dialog and compare it to a other field to check if there was any alterations/modifications..

let say that i have this:

var var1=_arrObjType1[0].Field1;
var var2= $("#EditText").val();

if after this i do an alert with both fields, they are empty..

alert(var1+ "---" +var2);

but when i try to print to the console by doing this:

console.log(var1.length);
console.log(var2.length);
console.log(var1);
console.log(var2);

in the console appears this

1
0
""

(an empty string)

both fields are empty and i am trying to compare to see if are equal or not...

Upvotes: 0

Views: 72

Answers (2)

Balachandran
Balachandran

Reputation: 9637

Try to use javascript's .trim(),

var var1=_arrObjType1[0].Field1;
var var2= $("#EditText").val();

if(var2.trim() === var1.trim()) {
  // both are equal
}

Upvotes: 1

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

Try to use $.trim() to cut off the leading and the trailing spaces and then compare them,

if($.trim(var1) === $.trim(var2)){
 //both are equal
}

Upvotes: 2

Related Questions