Anirudha Gupta
Anirudha Gupta

Reputation: 9289

Why isn't there any value for this variable in this jQuery code?

Consider:

var ret = { valid: true, message: "" };
var prtime = $(".ptime").val();
var ctime = $(".ctime").val();

if ($(prtime).length == 0 || $(ctime).length == 0) {
    ret = { valid: false, message: "" };
}

$(prtime).length

When I run this, I get 0, even when I fill some words in prtime. And when I do $(".ptime").val().length then it shows me the length.

What have I done wrong with that code?

Upvotes: 0

Views: 49

Answers (2)

Shadow Wizard
Shadow Wizard

Reputation: 66389

prtime is a plain string. Don't wrap it as a jQuery object.

To get the amount of characters in the string, have:

var myLength = prtime.length;

Upvotes: 1

dotnetstep
dotnetstep

Reputation: 17485

//Use this way
var ret = { valid: true, message: "" };     
var prtime = $(".ptime");  
var ctime = $(".ctime");     
if ($(prtime).length == 0 || $(ctime).length == 0) {     
 ret = { valid: false, message: "" };    
}

Upvotes: 0

Related Questions