-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

Community
  • 1
  • 1
shinzou
  • 5,850
  • 10
  • 60
  • 124

4 Answers4

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

sheetal
  • 565
  • 6
  • 13
1

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

append a . or # for class or ID

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

Rahil Lakhani
  • 412
  • 4
  • 7
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.

Dean James
  • 2,491
  • 4
  • 21
  • 29
-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);