76

I'm a bit new to jQuery so forgive me for being dense. I want to select all <td> elements on a particular page via Chrome's JS console:

$('td')

Yet when I do this, I get the following output:

<td>Apples</td>

Isn't jQuery supposed to return an array of elements with the <td> tag? Why am I only seeing the first element that matches this criteria?

Here is the site in question: http://www.w3schools.com/html/html_tables.asp

EDIT: I'd like to add that when I type a jQuery function into Chrome console, I do NOT get a jQuery object back. I get a plain HTML element. Something must be wrong with the way my Chrome is set-up/configured.

1
  • It indeed does select every element. element selector Commented Jan 13, 2013 at 21:53

4 Answers 4

156

If jQuery isn't present on the webpage, and of course no other code assigns something to $, Chrome's JS console assigns $ a shortcut to document.querySelector().

You can achieve what you want with $$(), which is assigned by the console a shortcut to document.querySelectorAll().

To know if the page contains jQuery, you can execute jQuery in the console. Note that the Q in command must be in uppercase.

To know if jQuery is assigned to $, you can execute $().jquery which will return jQuery version if that's the case. Note that the q in command must be in lowercase.

Also, there are browser addons to inject jQuery in every webpage.

Sign up to request clarification or add additional context in comments.

3 Comments

Minor correction: $ is NOT an alias for document.getElementById - it's an alias for document.querySelector. developers.google.com/web/tools/chrome-devtools/console/…
This correction is definitely not minor :-) Actually, the behavior has been changed. With a bit of googling I found WebKit ticket 92648. Thanks for pointing out this change.
If you want to operate on each one you can do [].forEach.call($$(".ClassName"), function(fe){ fe.click();})
17

It seems jQuery is not properly included to run on your target page. I had a similar problem and solved as follows for Google Chrome.

Add a bookmark to your Chrome browser containing the following one-liner code as the URL field (it's beautified for readability):

javascript: (function () {
    var s = document.createElement('script');
    s.setAttribute('src', 'https://code.jquery.com/jquery-latest.min.js');
    if (typeof jQuery == 'undefined') {
        document.getElementsByTagName('head')[0].appendChild(s);
    }
    jQuery("td.edit select option[value=BN]").attr("selected", "");
})();

Then just click that bookmark to run it. It is expected to include jQuery normally and make the console to return something like function (e,t){return new b.fn.init(e,t,r)} as you type in $.

The process of creating the bookmark (also called bookmarklet) is a short-hand for injecting jQuery in every page you wish to work on with the console. However, the snippet code also works if you copy-paste it directly into the JS console.

PS: Credits for the snippet is not mine, since I'm using it for a while and can't remember where I get that from.

Hope it helps.

4 Comments

How can I keep the code as url ?? The moment I press enter it is google searched ( I have the omni bar set to google search ).
Using Chrome, the moment the snippet is entered into the adress bar, the "javascript:" part at the beginning of the string is oddly omitted and the command is interpreted as a search term. I´ve tested with Firefox and that doesn´t happen though. Make sure you enter "javascript:" previous to function declaration when using the snippet directly into the adress bar.
Yeah that happens with me too...I added the 'javascript:' manually though but still on hitting enter nothing happens, the page reloads or something...there is nothing I can bookmark
The bookmark is a shorthand for storing the url for later use when you need to locally inject jquery into a given webpage you are viewing in case it was not loaded yet. After running the snippet (either by clicking your bookmarked code, typing it at the adress bar or even at the console), it is expected that jquery gets loaded for you to play around using the browser console. Nothing really happens with the page (in terms of requests) because it is a simple dynamic inclusion of the jquery library. Hope it helps.
4

If jQuery is installed and if the $ symbol is shorthand for jQuery, then $('td') returns a jQuery object. But, in the w3schools page you linked, I don't see that jQuery is even present.

If jQuery was present and the debugger has not overriden the $ symbol, then $('td') would return a jQuery object. The jQuery object is an array-like object (has some properties of an array), but it is not an actual array. If you are looking at things in the console, then you will have to make sure you are looking at the DOM elements themselves, not at the containing jQuery object.

If you want to get an actual array of DOM elements, you can do this:

$('td').get();

which will return an array of DOM elements.

If that doesn't work, then you should examine the timing of your call to $('td').get() to make sure that all td items you want are in the page before you search for them.

7 Comments

Perhaps the problem is with my Chrome configuration? When I type $('td') I don't get a jQuery object back... I just get a plain HTML element. Likewise when I type $('td').get() I get the error message TypeError: Object #<HTMLTableCellElement> has no method 'get'
@effbott - it may be that the Chrome debugger has overriden $ as a debugging shortcut. Try using jQuery('td').
jfriend00, that sounds about right. When I type jQuery('td') into the console I get the error message ReferenceError: jQuery is not defined. Perhaps I should just re-install Chrome? I didn't always have this problem.
@effbott - The w3schools page you linked does not appear to have jQuery installed on it. Thus, you can't use jQuery on that page.
"In that object is an array of DOM ids" - It's an array(-like object) of DOM elements, not DOM ids. (As I'm sure you know.)
|
0

Also if you are trying to do something with each of the td's you would need to use a .each() to loop through them. For example:

$('td').each(function() {
   //do something relevant here
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.