-3

I'm trying to get all the div classes in a page using jQuery $('div');, it's on a page that doesn't support jquery but I load it using the links below until I get $ === jQuery is true.

Even here on SO which has jQuery, when I do var x = $('post-text'); x is empty. What might be the reason for this not working?

I do this in Chrome's console.

How to run jQuery directly on any page in the browser?

testing jQuery statements in Chrome JavaScript console

6
  • post-text is not a tag name. Is the DOM loaded before you execute $('div');? Commented Sep 24, 2016 at 9:08
  • $('.post-text') this worked Commented Sep 24, 2016 at 9:11
  • @Xufox by DOM you mean to load jQuery? Commented Sep 24, 2016 at 9:13
  • No. The DOM is not jQuery. Commented Sep 24, 2016 at 9:14
  • @Xufox Well the page did load completely, so I assume the DOM did too. How do you check if it was loaded? Commented Sep 24, 2016 at 9:17

4 Answers 4

2

You must use '.' before the class name and '#' before the id name of element as $('.classname') for accessing any html element

You can add jquery by adding it's online link in your head tag and with without adding jquery in your page you can't use it's features

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

2 Comments

What about the div selector? Why that is empty?
because you need to add jquery in your page to get the element using $('element')
1

var x = $('post-text'); seems wrong

append a . or # for class or ID

var x = $('.post-text'); OR var x = $('#post-text');

Comments

0

Sounds like jQuery might not be included on the page. I have fallen victim to this before - Chrome console has a built-in selector engine also defined as $, meaning that when jQuery is not loaded on a page, $(...) can be used to do a DOM lookup.

Use $.fn.jquery in order to find out the version of jQuery loaded. It should return something like "1.7.1" but the version you have. If it isn't defined, jQuery isn't loaded.

5 Comments

I get 1.11.1 on that page from $.fn.jquery
Are you using the jQuery on ready function to make sure you're executing after jQuery is loaded? Also, when I do $('div') on SO I get results.
It's not on SO, I just tested it here. I load jquery using the links in my question and run the commands in chrome console.
Anyway looks like jquery does work there, I wasn't using the dot selector.
Sounds like @sheetal has the correct answer. Feel free to make it as the accepted answer :)
-2

first you add jquery file in page like this

var jquery = document.createElement('script'); 
jquery.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
document.head.appendChild(jquery);

now you can use all jquery functions

alert($('div').length);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.