rogerdpack
rogerdpack

Reputation: 66751

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?

Upvotes: 0

Views: 1923

Answers (5)

T.J. Crowder
T.J. Crowder

Reputation: 1074465

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.)

Upvotes: 5

Paul
Paul

Reputation: 141827

If you have included jQuery in your page, then your $ has been overwritten with a function that gets an element by Id and has nothing to do with jQuery. Use this:

(function($){
    // Now $ refers to jQuery

    $("#contains_filter_select").text("abc");

})(jQuery);

JSFiddle

If you have not included jQuery please see T.J. Crowder's answer.

Upvotes: 0

U.P
U.P

Reputation: 7442

use hash sign (#) with ids for selector

$("#contains_filter_select" )

Upvotes: 1

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114377

Selectors for IDs must be prefixed with "#".

$("#contains_filter_select") not $("contains_filter_select")

Upvotes: 0

ayyp
ayyp

Reputation: 6598

It should be $("#contains_filter_select" ) in order to get the <div> ID. It should work once you do that.

Edit: If you look here: http://jsfiddle.net/mRBJq/ it works fine.

Upvotes: 0

Related Questions