Check if an element exists with switch

Asked

Viewed 155 times

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?

  • 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...

  • 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 ?

  • 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.

  • 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..

  • 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.

Show 1 more comment

1 answer

5


The definition of the function is quite confusing. I believe you want to do the following:

function forumversion()
{
    if (jQuery('#ipbwrapper').length) return "invision";
    if (jQuery('.bodyline').length)   return "phpbb2";
    if (jQuery('#wrap').length)       return "phpbb3";
    if (jQuery('#pun-intro').length)  return "punbb";
    return "error";
}

Now on the switch you must compare the function invocation, not the function itself. forumversion will never be one of those strings, but forumversion() it’s gonna be. Wear it like this:

switch (forumversion()) {
case "invision":
    // Processamento aqui para esse tipo de forum
    break;
case "phpbb2":
    // Processamento aqui para esse tipo de forum
    break;
case "phpbb3":
    // Processamento aqui para esse tipo de forum
    break;
case "punbb":
    // Processamento aqui para esse tipo de forum
    break;
}

If you feel it necessary to add the defualt or a case "error" at the end of the options.

  • 1

    You were faster than me... you’re writing the same when I saw your answer.

  • @Vieira I read his comment on the question right after I pressed and send. It was too late.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.