Search code examples
jqueryprototypejs

how to get prototype div selector as object


I'm a noob to prototype/jquery, and when I try to do this in the chrome console, on a page with a div called "contains_filter_select":

>> $("contains_filter_select" )
<div id=​"contains_filter_select">​…​</div>​

but it appears to somehow not be an object?

>> $("contains_filter_select").text("abc")
TypeError: Object #<HTMLDivElement> has no method 'text'

 $("#contains_filter_select")
 null

I feel like I'm missing something trivial here, could somebody point me in the right direction?


Solution

  • You said:

    >> $("contains_filter_select").text("abc")

    TypeError: Object #<HTMLDivElement> has no method 'text'

    That suggests to me that you aren't using jQuery at all, but some other library. There are several libraries that use the $ symbol: jQuery, Prototype, and MooTools (at least). The way both Prototype and MooTools work, when you use $("contains_filter_select"), you'll get back a reference to the actual div object; jQuery is different, with jQuery you get back a wrapper around a matching set of elements (and the string would be a CSS-style selector, with a # before the element ID, unlike Prototype and MooTools where it's just an ID value). With jQuery, you wouldn't get the error you're getting, even if your selector didn't work. With Prototype or MooTools, I'm pretty sure you would, as neither of them adds a text method to elements.

    So the answer is: I don't think you're using jQuery on that page at all. Double-check the script tags. (FWIW, if you happen to be using the popular on-line tool jsFiddle, it defaults to MooTools if you don't change the drop-downs on the left.)