update the class of a div

J

Jean-Sébastien

Hello,
I'm starting in rails and ajax. So i've a javascript question:
someone could help me to update the class of a div?

Regards.
 
S

shimmyshack

Hello,
I'm starting in rails and ajax. So i've a javascript question:
someone could help me to update the class of a div?

Regards.
<html><body>

<div id="div_id"></div>
<style type="text/css">
..wow
{
border: 2px dashes red;
}
</style>
<script type="text/javascript>
var d = document.getElementById('div_id');
d.className += ' wow';
</script>
</body>

It hasn't anything to do with rails or ajax though, for that you will
use the rails helper scriptaculous.
 
B

Benjamin

Hello,
I'm starting in rails and ajax. So i've a javascript question:
someone could help me to update the class of a div?
document.getElementById("myDiv").className = "newClass_name";
 
L

-Lost

Jean-Sébastien said:
Hello,
I'm starting in rails and ajax. So i've a javascript question:
someone could help me to update the class of a div?

You use the className property of the element's instance. For example:

..c1 { background-color: purple; }
..c2 { color: red; }

<div id="d1">...</div>

document.getElementById('d1').className = 'c1';

Do not forget though, that the className property *replaces* it's value when written to.
Therefore if you would like to stack classes, you would need to:

document.getElementById('d1').className = 'c1';
document.getElementById('d1').className += ' c2';

Notice the plus and the space in the string literal.

-Lost
 
R

RobG

<html><body>

<div id="div_id"></div>
<style type="text/css">
.wow
{
border: 2px dashes red;}

</style>
<script type="text/javascript>
var d = document.getElementById('div_id');
d.className += ' wow';
</script>
</body>

It hasn't anything to do with rails or ajax though, for that you will
use the rails helper scriptaculous.

Not Scriptaculous, Prototype.js's addClassName and removeClassName
methods:

$(id).addClassName('blah');


However, other frameworks can be used with Rails, e.g. Fork's addClass
and removeClass:

<URL: http://www.forkjavascript.org/dom/docs >
 
L

-Lost

Not Scriptaculous, Prototype.js's addClassName and removeClassName
methods:

$(id).addClassName('blah');

However, other frameworks can be used with Rails, e.g. Fork's addClass
and removeClass:

jQuery has a similar method.

$('p[@id=p1]').addClass('className');

Will take a P tag with an attribute ID of p1 and add the class named className.

$('p.className').addClass('className2');

Which takes a P tag, with a class of className, and adds className2, to it.

Not knocking Michaux either!

-Lost
 
R

RobG

Not Scriptaculous, Prototype.js's addClassName and removeClassName
methods:

However, other frameworks can be used with Rails, e.g. Fork's addClass
and removeClass:
<URL:http://www.forkjavascript.org/dom/docs>

jQuery has a similar method.

$('p[@id=p1]').addClass('className');

Will take a P tag with an attribute ID of p1 and add the class named className.

$('p.className').addClass('className2');

Which takes a P tag, with a class of className, and adds className2, to it.

Can jQuery be used with Rails a la Prototype.js?
 
J

Jean-Sébastien

jQuery has a similar method.
$('p[@id=p1]').addClass('className');

Will take a P tag with an attribute ID of p1 and add the class named className.
$('p.className').addClass('className2');

Which takes a P tag, with a class of className, and adds className2, to it.

Can jQuery be used with Rails a la Prototype.js?

Thanks to all for your answers. It is really helpfull!
RobG, even if prototype and scriptaculous are in the default package
you can use other ajax library instead of this one. But i've never
done it. Maybe you should read this post :
http://yehudakatz.com/2007/01/31/using-jquery-in-rails-part-i/
and thanx again.
 
L

-Lost

RobG said:
It hasn't anything to do with rails or ajax though, for that you will
use the rails helper scriptaculous.
Not Scriptaculous, Prototype.js's addClassName and removeClassName
methods:

However, other frameworks can be used with Rails, e.g. Fork's addClass
and removeClass:
<URL:http://www.forkjavascript.org/dom/docs>

jQuery has a similar method.

$('p[@id=p1]').addClass('className');

Will take a P tag with an attribute ID of p1 and add the class named className.

$('p.className').addClass('className2');

Which takes a P tag, with a class of className, and adds className2, to it.

Can jQuery be used with Rails a la Prototype.js?

Yes, I believe so.

http://docs.jquery.com/Core#.24.noConflict.28.29

Illustrates jQuery playing nicely with other such libraries, namely Prototype.

Also, JQuails looks promising. Did not find too much information on it though, so cannot
be sure.

-Lost
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top