0

Having trouble loading jquery script in html page. When I debug certain values meant to become red but they don't. What am I doing wrong? FYI here is what I am trying to replicate in visual studio : http://jsfiddle.net/jfriend00/GAwrB/

    <html>
<head>
<title></title>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
    $(".colorMe td").each(function() {
        var valueInTd = $(this).text();
        if (valueInTd < 3000) {
            $(this).css('background-color', '#F00000');
        }
    });
});

</head>
<body>
 <table class="colorMe">
<tr><td>2000</td><td>3500</td></tr>
<tr><td>3000</td><td>2500</td></tr>
<tr><td>4000</td><td>4500</td></tr>
</table>​
  </body>
</html>
1
  • You have updated the code in your question to a version that works. Why? If you took advise from an answer below, please accept it as an answer. Also, unless what's showing now is actually your original code, please revert back to it or delete this question as it doesn't make sense at the moment. Commented Oct 24, 2012 at 17:41

4 Answers 4

1

I'm not sure what you're trying to do, looks like you're mixing an IIFE (immediately-invoked-function-expression) and a regular function declaration.

If you just paste your own code into JSFiddle you can see the errors: http://jsfiddle.net/f6sH6/

Here is the error I get: Uncaught ReferenceError: rr is not defined

Looks like you tried to over complicate the issue. This works fine:

$(function () {
    $(".colorMe td").each(function() {
        var val = parseInt(this.innerHTML, 10);
        if (val < 3000) {
            this.style.backgroundColor = "#F00000";
        }
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

@user1712552 Are you looking at your JS console? Because it's a valid error with respect to your code.
0

jQuery syntax is slightly different:

$(".colorMe td").each(
    var val = parseInt($(this).text(), 10);
    if (val < 3000) {
        $(this).css('background-color','#F00000');
    }
)

Comments

0

2 problems I can see. 1) your function declaration for rr is inside parenthesis so it will not be reachable outside of those, 2) the each function you're calling is using improper syntax. It should look like:

$(".colorMe td").each(function() {
    // ... code
});

1 Comment

Post the new code that you have as an update so we can see what else might be wrong.
0

Here is another example:

http://jsfiddle.net/GAwrB/92/

I found a problem on the jQuery version. You should try the latest version and use the onDomReady instead of onLoad, it worked for me with your example.

Regards

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.