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 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    setAttribute doesn't seem to work

    I've a page that contains a DIV whose id is 'divExposure', what I want to do is to set the "onscroll" event whenever user scroll on the div, to achieve this objective I write this code, but with no luck...

    can anyone help me out what I'm missing, below is the code

    <script language="javascript">

    document.getElementById( 'divExposure' ).setAttribute( "onscroll", "emp()" );
    function emp()
    {
    //here goes my function logic...
    }

    </script>
    to check whether the onsroll att. is set I've done this

    alert( String( document.getElementById( 'divExposure' ).onscroll ) );
    and this alerts with emp() written on it.

  • #2
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Which browser did you test in? I think that it would work in FF, but not in IE. I'm not 100% sure, but I think that you indeed are allowed to do this kind of thing although unusual, but who knows?

    Instead, normally you would probably be writing:
    Code:
    document.getElementById('divExposure').onscroll = emp;
    This should work in IE too.

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Attributes are one thing, event handlers are another thing. Your way changes the attribute, but not the event handler. To change the event handler, the cross browser way to do it is this way:
    Code:
    element.onevent=theFunctionYouWantToHandleTheEvent
    So...
    Code:
    document.getElementById('divExposure').onscroll=function(){
        emp();
    }
    function emp(){
        //here goes my function logic...
    }
    
    
    
    
    // or...
    document.getElementById('divExposure').onscroll=emp;
    function emp(){
        //here goes my function logic...
    }
    
    
    
    
    // or...
    document.getElementById('divExposure').onscroll=function(){
        //here goes my function logic...
    }
    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
    New Coder
    Join Date
    Dec 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks mate

  • #5
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,387
    Thanks
    32
    Thanked 288 Times in 282 Posts
    Just to clarify the issue, the short story is that it doesn’t work in Internet Explorer; setAttribute() is broken for event attributes in that browser. Based upon a look at Internet Explorer’s DOM, it will set the attribute, but will ignore the event.

    The more correct way to do it would be to use addEventListener(), but Internet Explorer doesn’t support that either.

    Code:
    document.getElementById("divExposure").addEventListener("scroll", emp, false);
    Thus, you end up using the traditional, non‐standard method koyama and liorean mentioned, which does work in Internet Explorer as well as in the browsers that understand the above methods.
    For every complex problem, there is an answer that is clear, simple, and wrong.


  •  

    Posting Permissions

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