1

I have written a script that checks a set of radiobuttons to be checked. But due to different possibilities different radiobuttons will show. Is there a way to suppress JavaScript errors when it pops undefined/getElementById is null? Something like the @-char does in PHP?

Update:

A bit more background info. I've made a website where users can submit images and another party for whom the images are can select their top 3 of the images. So each image has three radiobuttons. The difficulty here lies in the fact that the radiobuttons must be controlled dimensional (horizontal and vertical), because a submitted image may only be at place 1, 2 or 3. This is my working code. But adding many if(!var == undefined) doesn't make the code prettier. Therefor I'm wondering if there is something like @suppressMe is possible?

function HandleRadioButtons(id, type, idString, img)
{
    var idArray = idString.split("|");  
    var place1  = document.getElementById("G_" + id);
    var place2  = document.getElementById("S_" + id);
    var place3  = document.getElementById("B_" + id);
    var img1    = document.getElementById("Winner1");
    var img2    = document.getElementById("Winner2");
    var img3    = document.getElementById("Winner3");    

    switch(type)
    {
        case "G" :
            place2.checked = false;
            place2.disabled = true;
            place3.checked = false;
            place3.disabled = true;
            img1.style.background = 'url(' + img + ') no-repeat center center #FFF';
            break;
        case "S" :
            place1.checked = false;
            place1.disabled = true;
            place3.checked = false;
            place3.disabled = true;
            img2.style.background = 'url(' + img + ') no-repeat center center #FFF';
            break;
        case "B" :
            place1.checked = false;
            place1.disabled = true;
            place2.checked = false;
            place2.disabled = true;
            img3.style.background = 'url(' + img + ') no-repeat center center #FFF';
            break;
    }     

    var current1, current2, current3 = "";

    for(i = 0; i < idArray.length - 1; i++)
    {
        var place1 = document.getElementById("G_" + idArray[i]);
        var place2 = document.getElementById("S_" + idArray[i]);
        var place3 = document.getElementById("B_" + idArray[i]);

        if(place1.checked == true)
        {
            var current1 = idArray[i];            
        }

        if(place2.checked == true)
        {
            var current2 = idArray[i];            
        }

        if(place3.checked == true)
        {
            var current3 = idArray[i];            
        }
    }

    for(i = 0; i < idArray.length - 1; i++)
    {        
        var place1 = document.getElementById("G_" + idArray[i]);
        var place2 = document.getElementById("S_" + idArray[i]);
        var place3 = document.getElementById("B_" + idArray[i]);

        if(idArray[i] != id && idArray[i] != current1 && idArray[i] != current2 && idArray[i] != current3)
        {
            switch(type)
            {
                case "G" :
                    place1.disabled = false;
                    place2.disabled = false;
                    place3.disabled = false;
                    break;
                case "S" :
                    place1.disabled = false;
                    place2.disabled = false;
                    place3.disabled = false;
                    break;
                case "B" :
                    place1.disabled = false;
                    place2.disabled = false;
                    place3.disabled = false;
                    break;
            }
        }
    }   
}
2
  • 2
    Why don’t you check if they are undefined or the returned value is null? Commented Nov 4, 2010 at 21:49
  • 4
    Sounds like you'd be better off fixing the root of the problem instead. What I mean is that maybe there's a way to code it so that you can avoid it entirely. Either way, you should post your code. Commented Nov 4, 2010 at 21:49

3 Answers 3

4

You can easily test for a null or undefined value in JavaScript, as both these values are falsy:

var element = document.getElementById('some-id');
if (element) {
   element.value = 'Hello';
}

You could also consider using a try/catch block:

try {
   var element = document.getElementById('some-id');
   element.value = 'Hello';

   // ... the rest of your code here.
}
catch (e) {
   if (!(e instanceof TypeError)) {
      // The exception is not a TypeError, so throw it again.
      throw e;
   }
}

However be careful that the above will suppress all the TypeError exceptions and that might make your code more difficult to debug.

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

2 Comments

Okay, that's still with IF's thus.. Well, if there is no other option, that should do it ;) Thanks
I'll try that tomorrow, thanks! +1 from me, if it works you'll get the accept too ;) I'll check in tomorrow again.
1

Try this

var element = document.getElementById('some-id');
element?.value = 'Hello';

1 Comment

Note that optional chaining won't work in NodeJS before v 14.
0

you can check if buttons are existing by getElementById and then check its length. Are you using any framework?

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.