1

I have this code:

if ($('#txtEdit').val == '') {
    window.parent.$('#note' + appid + dbid).html('<img src="images/note_gray.png">');
} else if ($('#txtEdit').val != '') {
    window.parent.$('#note' + appid + dbid).html('<img src="images/note.png">');
};

I've also tried it with a simple "else" instead of the "else if". I have also tried val(null) instead of val == ''. In all instances, it will work one way, but not the other. For example, as it is now (above) if I put something in the text box (txtEdit) it will set the div to note.png. However, emptying the textbox will not change it to note_gray.png.

I am new to jquery/javascript. Sorry.

2 Answers 2

4

Neither of your comparisons is valid. val is a function that returns a string. It is not a property.

if ($('#txtEdit').val() == '') {
    window.parent.$('#note' + appid + dbid).html('<img src="images/note_gray.png">');
} else { // else if unnecessary
    window.parent.$('#note' + appid + dbid).html('<img src="images/note.png">');
};
Sign up to request clarification or add additional context in comments.

Comments

2

Call val()

if ($('#txtEdit').val() == '') {

with the parens, also, as a good practice, use === and !== to check for equality or not.

3 Comments

Carissimo Alberto, why is === better than == ? (thanks ina advance!)
@roXon - it isn't "better", nor "good practice" in this case. == is the equals operator, it uses the abstract equality comparison algorithm to compare the expressions being evaluated. It's often called a truthy comparison. === is the strict equals operator and uses the strict equality comparison algorithm, which includes the type of the expressions being compared, e.g. 0=='' and 0==false are both true, but 0==='' and 0===false are both false. See ECMAScript section 11.9.
@RobG: in this case it's not a good practice, I was talking in general.

Your Answer

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