#467 ✓help_request

$(element) causes error if 'element' is undefined

Reported by JohnSherlock | November 26th, 2008 @ 04:46 PM


I'm new to Prototype (it's great!) so I'm not sure if this is a bug or a 'feature'. Here's the problem:

If you use the $ notation to reference an element (for example $(myDiv) ), but the element doesn't exist, you get a javascript error saying 'myDiv' is undefined.

However, if I use document.getElementById('myDiv'), this returns null. I would expect $(element) to also return null in this case.

Apologies if this isn't actually a bug...

Thanks & regards,

John :)

Comments and changes to this ticket

  • Tobie Langel

    Tobie Langel November 26th, 2008 @ 04:52 PM

    • Tag cleared.
    • State changed from “new” to “help_request”

    You seem to be confusing JS variables with element ids. Please seek assistance directly from the mailing list. Thank you!

  • JohnSherlock

    JohnSherlock November 26th, 2008 @ 04:59 PM

    • Assigned user set to “Tobie Langel”

    Thanks for the reply Toby.

    However, in this case, I am dynamically creating a div with id 'myDiv' and then trying to reference it using $(myDiv). This works fine if i execute it after creating the div, but raises the error if I attempt to reference $(myDiv) before I have created it. However, document.getElementById('myDiv') returns null if I call it before creating the div.


    John :)

  • Juriy Zaytsev

    Juriy Zaytsev November 26th, 2008 @ 06:07 PM


    $(myDiv) and $("myDiv") are completely different "things" : )

    Former one is a reference while latter one is just a string. If you were to call $ with a string, you would see expected results (i.e. null if element is not yet attached to a DOM)

  • JohnSherlock

    JohnSherlock November 26th, 2008 @ 06:13 PM

    • Assigned user changed from “Tobie Langel” to “Juriy Zaytsev”

    Thanks Juriy!

    I figured out what I was doing wrong after posting - I just misunderstood how the $ notation was working.

    Thanks guys... apologies for the mistaken ticket...


Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins