views:

576

answers:

6

I have an h2 element with an id of title and I have the following script:

<script type="text/javascript">
$(document).ready(function(){

   $("#title").css("background-color","red");   

)};
</script>

The background-color is not changing though and I can't figure out why?

<html>
<head>
<title>Table Sorter</title>
<script type="text/javascript" src="/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="/js/jquery.tablsorter.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){

$("#title").css("background-color","red");  

)};
</script>
</head>

<body>
<h2 id="title">Table Sorter</h2>
</body>
</html>

<script type="text/javascript">
$(document).ready(function()
{
    $("#title").css("background-color","red");
    $("#myTable").tablesorter();
}
);
</script>
+1  A: 

Can you get it working without jQuery? Try:

document.getElementById("title").style.backgroundColor = "#F00";

instead of your current script. If this does not work, check that you have well-formed HTML.

UPDATE: now that you've posted your HTML, I can see that you need to use a script tag instead of a link tag to import jQuery

Eric Wendelin
I changed it to script but it still does not work.
Xaisoft
My folder structure is the html file is in a folder called Jquery and the Jquery file is int a subfolder called js under the Jquery folder.
Xaisoft
Ah, then you need to remove the beginning slash in the src attribute if your jquery import script.
Eric Wendelin
I just removed teh started slash, but is still not changing the background-color
Xaisoft
Well my best advice then is to find out the path to jquery really is. I'm guessing that it is not being loaded
Eric Wendelin
it worked after i removed the other script tag, but I am not sure why I can't have two script tags
Xaisoft
I'm guessing that something in the tablesorter is overridding something in jQuery.
Eric Wendelin
hmm, it's weird because there is a sample right on the tablesorter.com website.
Xaisoft
Does the sample also use jQuery 1.3.2?
Eric Wendelin
+1  A: 

Replace

<link type="text/javascript" src="/js/jquery-1.3.2.min.js"/>

with

<script type="text/javascript" src="/js/jquery-1.3.2.min.js"/>
Chetan Sastry
Tried that, but it still not working.
Xaisoft
Are you getting a javascript error?
Chetan Sastry
I am getting no javascript errors.
Xaisoft
@Chetan — Not all browsers allow self-closing script tags. You need to use "<script …></script>" instead.
Ben Blank
Ben Blank is right. Self-closing script tags are the devil. http://tiny.cc/U6dXa
Josh Stodola
+6  A: 

Replace

<link type="text/javascript" src="/js/jquery-1.3.2.min.js"/>

with

<script type="text/javascript" src="/js/jquery-1.3.2.min.js"></script>

You also have a syntax error in your jQuery function at the closing brackets. They should be

$(document).ready(function(){
  $("#title").css("background-color","red");
});

If that still does not fix your problem, then put an alert in there like this...

$(document).ready(function(){
  alert("Howdy!");
});

If you do not see the alert message, then your jQuery script is not loaded, which means the relative path in the SRC attribute is incorrect.

Josh Stodola
I put the </script> and that didn't work and I changed the bracket order and that didn't work.
Xaisoft
@Xiasoft: then your jquery is not loaded.
SilentGhost
Gah, beaten to it. >_<
strager
A: 

Ok, I figured it out and I don't know why it is was happening. Below the JQuery script tag, I had another script tag:

<script type="text/javascript" src="/js/jquery.tablsorter.min.js"/>

When I removed the above, it worked. But I don't know why?

Xaisoft
Please edit your original question. Do not make an answer to post and update. Thanks.
strager
because code in tablesorter produced an error, I guess
SilentGhost
The reason it worked when you removed that <script> tag is because your browser does not recognize you are closing the <script> tag. It reads the rest of your document up until the </script> as if it were Javascript code IF the document (in src) didn't load (which it probably did, which is
strager
hmm, I just downloaded tablesorter from tablsorter.com
Xaisoft
likely the reason you didn't get any Javascript errors with it included). Simply add a </script> to the end and remove the / in /> to fix it.
strager
strager, I just removed /> and replaced it with </script> and it still didn't work.
Xaisoft
@Xaisoft, That's odd. Do you have a live copy of your site which can be examined (or a full HTML example)?
strager
I will update the html on my original question.
Xaisoft
Update the html.
Xaisoft
Ok, removing the starting slash now made it work.
Xaisoft
+2  A: 

Here you go. Other people have pointed out some small problems you had, such as using a link tag where you need a script tag, etc. This code works for me:

<html>
  <head>
    <title>Table Sorter</title>
    <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function(){
        $("#title").css("background-color","red");
      });
    </script>
  </head>
  <body>
    <h2 id="title">Table Sorter</h2>
  </body>
</html>

It looks like there was a typo in your code at the end of your $(document).ready section where you had )}; instead of });. If you use Firefox you can open up the error console and view any Javascript errors or warnings.

nascent.cognition
Thanks, I got it working by removing the other script tag.
Xaisoft
+2  A: 

It seems you've made another typo:

<title>Table Sorter</table>
                     ^^^^^

Replace table with title:

<title>Table Sorter</title>
strager
I changed that, can't believe it missed that, but it still is not giving the h2 a background color. It only gives it a background color when I remove the other script tag.
Xaisoft
@Xaisoft, Then something is wrong with your script. Post the URL of the script so we can investigate.
strager
I downloaded the plugin from here : http://tablesorter.com/docs/
Xaisoft
@Xaisoft, I am unable to reproduce the bug: http://liranuna.com/strager/b/h2-so.html
strager
I got it to color by doing removing the beginning slash, but I can't get the table to sort that is provided on the webpage. Maybe I am doing the Jquery script wrong, I will update the script portion.
Xaisoft