SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)

    Body onClick, Except

    Hi.

    I have a simple drop down menu in a website.

    It does the typical... opens onMouseOver, closes when the body is clicked.


    <body onclick="resetDrawer();">
    The problem is one of the hidden drop downs has an input field and, of course, when someone clicks in the field to enter info the menu disappears... the input is part of the body after all.

    Is there some type of work-around to this??

    Is there something like...


    <body onclick, except when elementbyid = 'InputField'>
    All suggestions welcome. Thanks.




    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  2. #2
    Non-Member
    Join Date
    Aug 2006
    Location
    Kansas City
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <body onclick="javascript:document.getElementById("txtField")? return false: MethodName();">

    Just a shot in the dark

  3. #3
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Didn't work.

    I'm thinking now... is there a way to get the clicked item in a variable so I could have say...

    Code JavaScript:
    If ($clickedItem != elementById('InputField') )
       {
       Execute Reset
       }

    ??




    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  4. #4
    Non-Member
    Join Date
    Aug 2006
    Location
    Kansas City
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    function ResetDrawer()

    {
    if(!this.id == document.getElementByID("inputField"))
    {
    execute code
    }
    else
    {
    //invalid field
    }

    }

  5. #5
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    As a test I tried that out as follows...

    Code JavaScript:
    function resetDrawerInit()
    	{
    	alert(this.id);
    	if(this.id != document.getElementById('Drawer2_Articles'))
    		{
    		resetDrawer();
    		}
    	}

    ...and the body onclick calls that new resetDrawerInit(); function.

    It's not working, though, and I'm pretty sure it's because the "this" keyword is referring to the whole body rather than the specific object that is clicked.

    The body tag is the one with the onclick, after all. No matter what, the body is the HTML element that's calling the function.

    Thanks for the suggestions anyway.




    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  6. #6
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Well... regarding above...

    what's weird, though is that my open body tag now looks like...


    Code HTML4Strict:
    <body id="Bob" onclick="resetDrawerInit();">

    ...so theoretically the alert in resetDrawerInit(); should say "Bob". In reality it says "undefined".

    Maybe I'm just using the this keyword wrong.




    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,786
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    JavaScript event handlers in the body tag are attached to the window and not to the body.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall
    JavaScript event handlers in the body tag are attached to the window and not to the body.
    Ore okay.

    So then there's no way of detecting which object was clicked on like that then??...

    because no matter what, "this" would refer to the window itself and not the individual clicked object. Yes??




    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse


Bookmarks

Posting Permissions

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