0
Hello, how do I make a check and check if certain element exists using switch? To be more specific, I’m trying to use this code:
var forumversion = function()
{
invision = jQuery('#ipbwrapper').length ? "invision" : "error";
phpbb2 = jQuery('.bodyline').length ? "phpbb2" : "error";
phpbb3 = jQuery('#wrap').length ? "phpbb3" : "error";
punbb = jQuery('#pun-intro').length ? "punbb" : "error";
}
switch( forumversion )
{
case invision: "Invision";
break;
case phpbb2: "phpBB2";
break;
case phpbb3: "phpBB3";
break;
case punbb: "punbb";
break;
default: "Erro ao identificar a versão do fórum"
}
In this case I want to check which version of certain forums.. To do this I check if certain specific elements of each version exist (within var forumversion are the elements..)..
I’m new with switch yet, where I could be missing?
I couldn’t quite understand the goal, but in your case it would not be better to use
if
?– Vieira
Yes, with if I know how to do.. But just wanted to know out of sheer curiosity, learn more things and new ways to do too... About the goal is to check which version of a forum (of a particular platform) with Javascript, each platform has a different HTML structure, checking if a certain element exists I know which platform...
– Wagner'
I think you didn’t get a good case for switch. Switch is good for when Voce has only one value, and want to make several comparisons at this same value. In your example you have too different conditions. What if the person has invision and phbb3 ?
– Bacco
I agree with Bacco, this is a very bad case for a switch, but for the sake of learning, I will show you how you could do, but for this much will have to be changed... I’ll rewrite your code in one reply.
– Vieira
It was only for me to have a sense of how friends would be made... In this case there is no possibility of him using more than one version. Thank you for your attention, the code worked perfectly..
– Wagner'
The answer was depending on if and used the switch as mere Dispatcher. It serves as an example of the switch working, since the purpose of the question was this, but note that on a day-to-day basis the switch would probably not be a very suitable way to solve a similar problem.
– Bacco