75

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.

rdonatoiop
  • 1,185
  • 1
  • 14
  • 28
fbonetti
  • 6,652
  • 3
  • 34
  • 32
  • It indeed does select every element. [element selector](http://api.jquery.com/element-selector/) – Fallup Jan 13 '13 at 21:53

4 Answers4

149

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. To know if jQuery is assigned to $, you can execute $().jquery which will return jQuery version if that's the case.

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

Gras Double
  • 15,901
  • 8
  • 56
  • 54
  • 4
    Minor correction: `$` is NOT an alias for `document.getElementById` - it's an alias for `document.querySelector`. https://developers.google.com/web/tools/chrome-devtools/console/command-line-reference#queryselector – fbonetti Mar 28 '17 at 15:58
  • 4
    This correction is definitely not minor :-) Actually, the behavior has been changed. With a bit of googling I found [WebKit ticket 92648](https://bugs.webkit.org/show_bug.cgi?id=92648). Thanks for pointing out this change. – Gras Double Mar 28 '17 at 22:48
  • If you want to operate on each one you can do `[].forEach.call($$(".ClassName"), function(fe){ fe.click();})` – Mark May 10 '21 at 11:42
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.

Pikamander2
  • 7,332
  • 3
  • 48
  • 69
rdonatoiop
  • 1,185
  • 1
  • 14
  • 28
  • 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 ). – akki May 08 '14 at 10:59
  • 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. – rdonatoiop May 08 '14 at 12:10
  • 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 – akki May 08 '14 at 12:21
  • 1
    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. – rdonatoiop May 08 '14 at 13:20
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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 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 # has no method 'get'` – fbonetti Jan 13 '13 at 21:55
  • @effbott - it may be that the Chrome debugger has overriden `$` as a debugging shortcut. Try using `jQuery('td')`. – jfriend00 Jan 13 '13 at 21:56
  • 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. – fbonetti Jan 13 '13 at 22:01
  • 1
    @effbott - The [w3schools page you linked](http://www.w3schools.com/html/html_tables.asp) does not appear to have jQuery installed on it. Thus, you can't use jQuery on that page. – jfriend00 Jan 13 '13 at 22:14
  • _"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.) – nnnnnn Jan 13 '13 at 22:17
  • This is another example of why I tell people not to use the w3schools website. They have stacks of mistakes and have nothing to do with the standards organisation. Rather use the jQuery API site for jQuery examples (http://api.jquery.com/) and the w3 consortium's website for CSS/HTML standards and examples (http://www.w3.org) – Zappa Jan 13 '13 at 22:30
  • @nnnnnn - OK, I tweaked my wording to include "an array-like object". – jfriend00 Jan 13 '13 at 22:40
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
});
Zappa
  • 453
  • 1
  • 5
  • 14