A JavaScript question. [more inside]
Asked by
jrpowell (
40562)
March 5th, 2008
I’m hoping to copy and paste together a greasemonkey script. I know nothing of JavaScript..
So what I need..
JavaScript that can find all elements with a CSS class. class=“whatever”
I would like to add a CSS style to any <tr> elements that have that class. The end goal is having the row in the table highlighted with a background color when I hover over it.
And I suck at JavaScript..
Observing members:
0
Composing members:
0
7 Answers
I can’t answer the question directly. But here’s a CSS-only solution that allows for that functionality, I think: http://cssglobe.com/lab/tablecloth/
Scroll to the bottom of the article to change the settings so , for instance, only table rows highlight when moused over.
Thanks.. But it needs to be JavaScript because I want to modify a existing web page.
All the big JS libraries (jQuery, YUI, scripty) have CSS selection built in. You might want to try jQuery—it’s very lightweight.
You can also just strip out the CSS selection from EXT, we did that for a little bit.. but that’s a bit more involved.
Untested (and, convert curly apostrophes to straight tick marks):
var trs = document.getElementsByTagName(‘TR’);
for (var i = 0, len = trs.length – 1; i < len; ++i) {
if (trs[i].className.match(/myClassName/)) {
trs[i].onmouseover = function () {
this.className = this.className + ’ ’ + ‘hoverClass’;
}
trs[i].onmouseout = function () {
this.className = this.className.replace(/hoverClass/, ’’);
}
}
}
Something like that. It would be easier with a JavaScript library as the starting point.
Thanks for the info… I think it might be time to learn Javascript. I am still pissed about what Javascript did to the Internet in 1998. I have been hesitant to learn it.
I’m not sure about folding a JS library into a Greasemonkey script.
I’m off to Amazon to buy a “Dummies” book.
What did JavaScript do to the internet in 1998? :-)
Answer this question
This question is in the General Section. Responses must be helpful and on-topic.