943,169 Members | Top Members by Rank

Ad:
Aug 30th, 2010
0

jQuery if statement trouble

Expand Post »
Hey, how's it goin

I'm trying to write a simple if statement with jQuery...it looks like this

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. if ($('#title .general').length == 0) {
  2. function() {
  3. $('#ubi').empty();
  4. $('#address').empty();
  5. $('#city').empty();
  6. $('#region').empty();
  7. }
  8. }

First off, i'm getting an error in firefox that has

"syntax error"
function( ) { }
-----------^

Second, I have some elements that are to be hidden and then made visible after certain things happen on the page, but once I introduce this if statement, all elements appear visible on document ready, as if all the code after the if statement that pertains to those elements is invalidated. Err...what the if!

So I believe I need to pass in a parameter? I'm not sure what that would need to be, i'm kind of cloudy on the idea of parameters for some reason. Anyone able to point me in the right direction?

Mucho gracias.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
seniramsu is offline Offline
6 posts
since May 2010
Aug 30th, 2010
0
Re: jQuery if statement trouble
You can't define a function within a conditional statement.

You would do this:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function empty_fields() {
  2. $('#ubi').empty();
  3. $('#address').empty();
  4. $('#city').empty();
  5. $('#region').empty()
  6. }
  7.  
  8. if ($('#title .general').length == 0) {
  9. empty_fields();
  10. }
Last edited by pwinfrey; Aug 30th, 2010 at 3:36 pm.
Reputation Points: 15
Solved Threads: 6
Newbie Poster
pwinfrey is offline Offline
18 posts
since Jul 2010
Aug 30th, 2010
0
Re: jQuery if statement trouble
Ok great, thanks! Unfortunately my problem extends further than just the correct syntax of the if statment.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
seniramsu is offline Offline
6 posts
since May 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: I'm getting this error Expected '}'
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: jQuery function executing, but not at the right time





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC