3

i'm trying to make a timetable for a Moodle in form of a Greasemonkey script.

The idea is to highlight the subject for the current hour, but i can't find a working way of doing it.

I'm injecting css into the page, and using jQuery to add the injected css class to the target td.

This is the css code injected into the page header:

.current_class {background-color:green};

And this is the javascript code used to add the class to the td:

var cell = $("#timetable_table").find("tr").eq(current+1).find("td").eq(date.getDay());

I know cell is the correct td because i can cell.text("foo") and the correct cell is modified, but when i do:

cell.addClass("current_class");

the table stays the same.

I don't have to do it that way, i only want to dynamically change the background on a td tag, so i don't mind using other methods.

Thanks.

9
  • 2
    could you provide some of your table html? Commented May 14, 2013 at 18:43
  • Can you post a fiddle with your HTML table, CSS and JS too? Commented May 14, 2013 at 18:44
  • Where do current and date come from? Commented May 14, 2013 at 18:45
  • Can you confirm that the class attribute of the element is changed? Your CSS should have the semicolon immediately after green. Commented May 14, 2013 at 18:46
  • 3
    Your code runs fine for me: jsfiddle.net/tC8vL You need to add more context like is there other classes added to the cells? Commented May 14, 2013 at 18:47

2 Answers 2

7

First I would suggest you change to this:

var cell = document.getElementById('timetable_table').rows(current+1).cells(date.getDay());

And:

cell.className += (cell.className ? " " : "")+"current_class";

But that's just for performance reasons

The probable cause of the issue is that you have background-color being defined elsewhere with greater specificity. Try using this CSS selector:

#timetable_table tr>td.current_class{background-color:green};

That should be specific enough to win.

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

3 Comments

Could be the problem, becouse as stated before, i'm modifying a page, so might be alredy defined. But i dont understand the selector: it selects td with the class alredy?
CSS updates in realtime. So if you add a class to a cell in that table, it will match the selector and the new style will be applied.
so you are telling me to .addClass() and then to use the selector to add the background-color prop?
2

use className property for change or add class like example:

tbody.firstChild.lastChild.className='tr-parent1';

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.