Thread: Event Model

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Event Model


    Hello everyone,

    I have signed up for this forum so that I can reach out to more experienced users because I am try to complete homework that I am having a hard time with. I am not trying to get someone to complete my work for me, but am trying to see if someone could help me figure out how to code a function used in my homework for my Javascript class.

    The directions say to create a setup() function and that its purpose is to set up the web and define its handlers. Here are the commands it tells you to add to the function, which I am having trouble with.

    1. Point the scrollButton object to the page element with the ID button. (scrollButton is a global variable that I have already created in previous steps)

    2. Retrieve and set the top style value of scrollButton, using the getStyle() function. (I think this would be scrollButton.style.top = getStyle(scrollButton, "top")

    3. Define the cursor style of scrollButton to use the pointer cursor. (I think this would be scrollButton.style.cursor = cursorType

    4. Use the addEvent() function you created for the flibrary.js file to assign the grabIt() function, which you will create shortly, to the mousedown event occuring within the scrollButton object. Assume that the event handler is assigned during the bubbling phase. Here is the addEvent() Function I already created:

    function addEvent(object, evName, fnName, cap) {
    if (object.attachEvent)
    object.attachEvent("on" + evName, fnName);
    else if (object.addEventListener)
    object.addEventListener(evName, fnName, cap);
    }

    5. Use the addEvent() function again to assign the keyShow() function to the web document in response to the keydown event. Again, assume that the event handler us assigned during the bubbling phase. (The keyShow() function is created later on.)

    I would greatly appreciate any help that I can get!

    Thanks,
    John
  2. #2
  3. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,124
    Rep Power
    4304
    Welcome to DevShed Forums, john3j.

    What is your question though? Which parts are unclear to you? For example, have you used the addEvent() function before?

    While I'm at it, I'll say that that addEvent() function is not very good (which is not surprising because most text books for HTML, CSS, and/or JavaScript are outdated by the time any student reads it). The first reason is that unless there is a good reason not to, the standards-based method, e.g. addEventListener, should be used first instead of the alternative(s), which in this case is attachEvent. Secondly, since IE doesn't support the capturing phase anyway, there's not much point in having a parameter for that.
    Code:
    function addEvent(object, evName, fnName) {
      if(object.addEventListener)
        object.addEventListener(evName, fnName, false);
      else if(object.attachEvent) 
        object.attachEvent("on" + evName, fnName);
    }
    P.S. It would be helpful if you put your code between [code][/code] tags in the future.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    clarification


    Kravvitz,

    Thank you for the welcome to the forum. My question is "does the code below satisfy the requirements that I recently posted?"

    Code:
    function setup() {
    scrollButton = document.getElementById(id);
    scrollButton.style.top = getStyle(scrollButton, "top");
    scrollButton.style.cursor = cursorType;
    addEvent(scrollButton, "mousedown", grabIt, false);
    addEvent(scrollButton, "keydown", keyShow, false);
    }
    The addEvent function is what our book gave us, so that is all I know to use. Please let me know if you see any errors in my code?

    Thanks!
    John
    Last edited by Kravvitz; September 17th, 2012 at 08:33 PM. Reason: must we quote the whole post? :p
  6. #4
  7. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,124
    Rep Power
    4304
    It depends on what they mean by the first instruction. Since I'm reading it outside the context of the previous lessons in the book, I can't really tell.

    Have the variables "id" and "cursorType" been defined as strings with the appropriate values elsewhere in your code?

    Have you opened the page in your web browser to see if it works?

    The addEvent function is what our book gave us, so that is all I know to use.
    Yeah, that's what I assumed.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).

IMN logo majestic logo threadwatch logo seochat tools logo