0

I'm having an issue. I have a HTML page that has a javascript code and a jQuery code in the same script tag.

Here is my html with javascript and jQuery:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="description" content="">
  <meta name="author" content="">

  <title>Page</title>

  <!-- css -->
  <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css">
  <link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
  <link href="css/nivo-lightbox.css" rel="stylesheet" />
  <link href="css/nivo-lightbox-theme/default/default.css" rel="stylesheet" type="text/css" />
  <link href="css/animate.css" rel="stylesheet" />
  <link href="css/style.css" rel="stylesheet">

  <!-- javascript and bootstrap -->
  <script src="js/jquery.min.js"></script>
  <link href="js/bootstrap.min.js" type="text/javascript">
  <link href="js/modal.js" type="text/javascript">

  <!-- template skin -->
  <link id="t-colors" href="color/default.css" rel="stylesheet">


  <script>

    <!-- HERE IS SOME PURE JAVASCRIPT CODE -->

$(document).ready(function() {
  var table = $('#tableBody tr td');

  table.on("click",function() {
    table.removeClass("test-class");
    $(this).addClass("test-class");
  });
});
</script>

My problem is: when the page loads, my jQuery function do not work, but when I copy this function and paste it on the browser's console, it works perfectly.

NOTE: when I put an alert on the browser's console it works. That means my jQuery is loading with the page.

What I am missing?

Thanks in advance.

12
  • 1
    Check your browser's JavaScript console for errors. Commented Apr 22, 2015 at 16:22
  • 6
    you should be using script tags for javascript files and not link tag Commented Apr 22, 2015 at 16:23
  • 2
    Is there a table element on the page;.... or anything with that ID? Commented Apr 22, 2015 at 16:23
  • Yes, there is a table with that ID. Commented Apr 22, 2015 at 16:25
  • 1
    Pare your code down to a bare minimum that demonstrates the issue. Get rid of nivo, bootstrap, etc. and see at what point the error emerges. Can you duplicate the issue with a jsFiddle? Commented Apr 22, 2015 at 16:43

2 Answers 2

2

try the code

$('document').on("click",'#tableBody tr td',function() {
  var table = $('#tableBody tr td');
  table.removeClass("test-class");
  $(this).addClass("test-class");
});

i dont know if td element is created in code or it is the static html. If td or table is dynamic, DOM does not know where it is because when browser runs, it always keeps record of your original html.

DOM sometimes lose its tracking, so using the format on('click','#target', func()) will make DOM go back and find for the target again.

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

1 Comment

Yeah, that's it.. It solved my problem, One more lesson learned!! my table is dynamic. Thank you very much.
0

It's probably because the browser's Console has its own dollar sign object "$", at least Firefox and Chrome does.

From https://developer.chrome.com/devtools/docs/console :

$()

Returns the first element that matches the specified CSS selector. It is a shortcut for document.querySelector().

$$()

Returns an array of all the elements that match the specified CSS selector. This is an alias for document.querySelectorAll()

$x()

Returns an array of elements that match the specified XPath.

To check if jquery is loaded you can execute this in the browser Console:

typeof jQuery

If it's loaded the answer would be

"function"

2 Comments

my jQuery is loaded, i have made this test.
Ok, then the question is, how do you know that your jQuery code does not work? Could you put console.log('hello'); right under your line $(document).ready(function() { and check the console output ?

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.