0

I am writing a if/else statement that includes css in it. The if part works but the else part does not. What am I doing wrong, can someone guide me thanks?!

This is my statement.


if(!$(#ron.val()){
   $("#ron_lan").attr("disabled",true).css({"opacity":0.3});
}
else{
   alert('else');
   $("select#ron_lan").attr("disabled",false).css({"opacity":1});    
}
2
  • What IDE are you using? Commented Jul 5, 2015 at 21:58
  • i don't think i'm using any IDE...i'm just doing this on eclipse with apache. Commented Jul 5, 2015 at 22:16

2 Answers 2

5

$(#ron.val() is invalid and will always be false. It should be

$('#ron').val()

Notice the brackets and quotes. #ron is the selector in this case.

Also, don't use attr('disabled', false). You should just remove the attribute entirely with removeAttr('disabled')

Sign up to request clarification or add additional context in comments.

3 Comments

sorry had a mistake in typying...i do have it like you mentioned. $(!("#ron").val()) still not working
Care with that syntax, it should be if( !$("#ron").val() ).... @ron
thanks! Still the else part is not getting triggered.
1

Depends on what the value of #ron is. If it is an input the possible values are text, empty or spaces.

For example:

!""             // Negating an empty string gives you TRUE
!" "            // Negating a space gievs you FALSE
!"any text 123" // Negating any text gives you FALSE
!undefined      // Negating undefined gives you TRUE, read on...

It can also depend on the existance of #ron. If the element with id #ron does not exist, or does not have a "value" property (is not an input). jQuery will always return undefined which could explain why !undefined will always return true and your else path is never reached.

Recap:

  1. Check if #ron exists on the html eg. $('#ron').length should return 1
  2. Check that #ron is an input or textarea so that $('#ron').val() wont return undefined
  3. Check the values in the textarea and trim it to avoid spaces vs empty string scenarios

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.