We help IT Professionals succeed at work.

if in return statement

Refael
Refael asked
on
Hello Experts,

In the code below i am trying to include an if statement so if it returns any of the given strings (e.g. 07, 17)  then hide "seb" and show "tos" BUT if not then do something else....

The problem is i am always getting an error while trying to include the "IF" right after the "return" I am guessing it is the wrong place or i should learn JavaScript better :-)

Any help ..... :-)


$("table tr td").each(function() {
  		$(this).filter(function() {			
			var thisText = $(this).text();
	  		return thisText == "07" || thisText == "17"
		}).parent().hover(function() {
	  		$(".hause-seb").hide();
			$(".house-tos").show();
		});
	});

Open in new window

Comment
Watch Question

Freelance IT Consultant
Commented:
Since you seem only need to check the text of table cells, so try to use plain if statement without filter function. For example:
        $("table tr td").each(function() {
                var thisText = $(this).text();
                if (thisText == "07" || thisText == "17") {
		        $(this).parent().hover(function() {
	  		    $(".hause-seb").hide();
			    $(".house-tos").show();
		        });
                }
                else {
                        // do something else when text is not 07 or 17 here
                }
	});

Open in new window

Author

Commented:
Hi Duy Pham

For some reason the script is not working when else should execute.

Author

Commented:
Thank you Duy Pham.

I just needed to include the function again inside the else for it to work :-)
Thank you!
Duy PhamFreelance IT Consultant

Commented:
What was the issue with else? Have you managed to get it to work?

Author

Commented:
Hello Duy Pham,

I only needed to include the function once more right after the "else"... :-) Thank you!

$("table tr td").each(function() {
                var thisText = $(this).text();
                if (thisText == "07" || thisText == "17") {
		        $(this).parent().hover(function() {
	  		    $(".hause-seb").hide();
			    $(".house-tos").show();
		        });
                }
                else {
                        $(this).parent().hover(function() 
                        // do something else when text is not 07 or 17 here
                       });
                }
	});

Open in new window