Okay, I'm going to go out on a limb here and disagree with Bill the Lizard and Jon Skeet. So: Shields to maximum. :-) But I've answered a lot more javascript
questions than they have, so...
No, it's not okay.
I'll use jQuery as an example because that's where this mostly comes up.
If someone asks a question not using jQuery and without tagging the question jquery
, it's not okay to provide an answer that only helps them if they use jQuery.
It is, however, perfectly acceptable to provide an answer saying (or preferably showing) how to do it without jQuery and also, if relevant, pointing out to the person asking the question that they can probably save a lot of time and trouble if they use a library to smooth out browser differnces, provide more concise ways of doing things, etc., allowing them to concentrate on the thing they're actually trying to do.
Don't do this:
Question:
How can I change the color of a div
to red?
Answer:
Using jQuery:
$("selector_for_your_div").css("color", "red");
That's completely useless to the person asking the question if they're not using / don't want to use a library, or if they're using Prototype, or YUI, or...
Do this:
Question:
How can I change the color of a div
to red?
Answer:
You can set style information for an individual element by getting a reference to the DOM element and using its style
property. That's an object with properties for style information, including a color
property. So:
element.style.color = "red";
You get a reference to the DOM element for the div
in various different ways depending on how you can identify it. If it has an id
value, it's easy:
document.getElementById('the_id').style.color = "red";
...but if it doesn't, you might need to use getElementsByTagName
and various other DOM methods (links: core methods, html-specific, latest stuff) to find the element.
Off-topic: A lot of this stuff is a lot easier if you use a library like jQuery, Prototype, YUI, Closure, or any of several others. These sorts of libraries smooth out browser differences and offer a lot of useful functionality you can leverage, so you can concentrate on what you're actually trying to do rather than browser quirks and such. For instance, with jQuery you can do it like this:
$("selector_for_your_div").css("color", "red");
...where selector_for_your_div
can be any CSS3 selector (and jQuery adds some useful things of its own). Other libraries will have similar ways of simplifying things.
That's what I do (3,041 javascript
questions answered so far...). Yes it's more work. Tough. :-) Keep a few DOM links handy and it's actually fairly easy. If you can't solve the problem without using a library, either don't answer the question, or keep your answer general and conceptual, and then (again) point out how much easier your life can be if you use a library, and reference several of them.
Note: I use jQuery in nearly all new browser-based projects I do, only not using it if there's a very good reason. It's a great library. It's not without its faults, but what is? It works very well in general, it's well-supported, has good sponsors (e.g., is unlikely to become moribund and outdated), and there are thousands of plug-ins for it (90% of which are rubbish; but 10% of thousands is still a lot of useful stuff). So I'm just saying, I'm not (remotely) a jQuery-hater.