Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post

    document.getElementById not working in Opera

    Hi all,
    i have an array of checkboxes and i have Check All | Uncheck All links which on click will check all or unchecks all the checkboxes.

    Following is the checkbox:
    Code:
    <input type="checkbox" id="Item_ID[]"  name="Item_ID[<?=$key?>]" <?=$checked?> value="<?=$iarr['pk']?>">
    following is the link code and javascript :
    Code:
    <a href='' onclick="setCheckboxes(true) ; return false;">  Check All</a> <br> <a href='' onclick="setCheckboxes(false); return false;"> Uncheck All</a>
    
    <script language="javascript">
    
    
    function setCheckboxes(do_check)
    {
    	
    	var elts= document.getElementById('Item_ID[]');
    //	alert(elts);
    	elts_cnt = elts.length;
    //	alert(elts_cnt);
        if (elts_cnt>0) {
            for (var i = 0; i < elts_cnt; i++) {
    
                elts[i].checked = do_check;
            } // end for
        } else {
            elts.checked        = do_check;
        } // end if... else
    
        return true;
    } // end of the 'setCheckboxes()' function
    
    </script>
    But the above script doesnt work in Opera . it works fine in IE. how can i modify the script to make it work in all the browsers. i cannot change the name or id of the checkboxes.

    Thanks in advance
    Last edited by bhakti_thakkar; 04-30-2008 at 08:23 AM.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    I believe [ and ] are invalid characters for id. Just validate your code and correct all markup errors first, to correct your javascript
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Only a single element on a single page may have any given ID. If you have more than one element with the same ID, you should not expect getElementById to work at all.

    In this example, you are taking the result of getElementById and treat it as if it was a NodeList - it's not, that's in fact a browser bug. The getElementById method should always return a single Element. If your document does not make sure that there is only a single element with any given ID, then that is a broken document, but Opera does actually work perfectly fine as expected, in difference to Internet Explorer in this case. (If you're expecting Opera to have the Internet Explorer behaviour, then it's your expectation that needs to be corrected, because that's not the way things are meant to work.)




    Quote Originally Posted by bhakti_thakkar View Post
    But the above script doesnt work in Opera . it works fine in IE. how can i modify the script to make it work in all the browsers. i cannot change the name or id of the checkboxes.
    I'm sorry, but that attitude is really bad. Why? Because the correct way to fix your problem is to first make sure you never have more than a single element with the same ID. (Also, neither [ nor ] is allowed in an ID.)

    Second, if you want a NodeList of form controls (in this case checkboxes or radio buttons) that belong together, then you give all those elements the same name.



    That's the rules of the game. If you don't want to play by them, then you find yourself with scripts that never were meant to work the way they do, they just incidentally happen to play to some browser bugs in certain browsers which allow them to work in that particular browser.
    Last edited by liorean; 04-30-2008 at 08:47 AM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #4
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post
    hi liorean,
    thanks for your advice. i changed the ids and now its working fine.

    <input type="checkbox" id="Item_ID_<?=$key?>" name="Item_ID[<?=$key?>]" <?=$checked?> value="<?=$iarr['pk']?>">

    what you say is absolutely right. ID's are meant to be unique


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •