![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
"Prisoner at War" <prisoner_at_war (AT) yahoo (DOT) com> wrote in message news:c86208c9-45fb-4419-acb6-6d62c94d4ef0 (AT) m44g2000hsc (DOT) googlegroups.com... Okay, Folks, I guess my real burning concern all along is a "high-level" one: just how does JavaScript interact with CSS? [...] Basically, you get the element in Javascript, and set its style property. div id="content">blah</div javascript: var contentDiv = document.getElementById( "content" ); contentDiv.style.marginLeft = "1em"; Generally (there may be exceptions but I'm not aware of any) the property of the object corresponds to the name of the CSS property but camel cased. |
#2
| |||
| |||
|
|
On 2008-04-27, Nik Coughlin <nrkn.com (AT) gmail (DOT) com> wrote: "Prisoner at War" <prisoner_at_war (AT) yahoo (DOT) com> wrote in message news:c86208c9-45fb-4419-acb6-6d62c94d4ef0 (AT) m44g2000hsc (DOT) googlegroups.com... Okay, Folks, I guess my real burning concern all along is a "high-level" one: just how does JavaScript interact with CSS? [...] Basically, you get the element in Javascript, and set its style property. div id="content">blah</div javascript: var contentDiv = document.getElementById( "content" ); contentDiv.style.marginLeft = "1em"; Generally (there may be exceptions but I'm not aware of any) the property of the object corresponds to the name of the CSS property but camel cased. Yes. Another way to do it is to change the class attributes of elements with setAttribute. Then you can organize the styles in style sheets rather than packing them all into the elements' style attributes, and just switch the elements' classes so they get selected by different selectors. This kind of thing: .active { border: 2px solid red; background-color: yellow; color: black; etc. .. } contentDiv.setAttribute("class", "active") |
#3
| |||
| |||
|
|
Yeah, be careful doing this though. *Imagine you've got: div id="contentDiv" class="class1 class2 inactive">bhakjfh</div You want to switch between active and inactive and you do: contentDiv.setAttribute("class", "active") and suddenly it's: div id="contentDiv" class="active">bhakjfh</div Whereas what you really want is: div id="contentDiv" class="class1 class2 active">bhakjfh</div So you probably want to use the split method on contentDiv.className instead to get an array of class names, then replace inactive with active, glue the strings in the array back together and reassign it |
#4
| |||
| |||
|
|
Or do: function activate_item(){ this.className=this.className.replace(/passive/,'active'); // change passive to active } function passivate_item(){ this.className=this.className.replace(/active/,'passive'); // change active to passive } |
#5
| |||
| |||
|
|
Ben C wrote: On 2008-04-27, Nik Coughlin <nrkn.... (AT) gmail (DOT) com> wrote: "Prisoner at War" <prisoner_at_... (AT) yahoo (DOT) com> wrote in message news:c86208c9-45fb-4419-acb6-6d62c94d4ef0 (AT) m44g2000hsc (DOT) googlegroups.com... Okay, Folks, I guess my real burning concern all along is a "high-level" one: just how does JavaScript interact with CSS? [...] Basically, you get the element in Javascript, and set its style property. div id="content">blah</div javascript: var contentDiv = document.getElementById( "content" ); contentDiv.style.marginLeft = "1em"; Generally (there may be exceptions but I'm not aware of any) the property of the object corresponds to the name of the CSS property but camel cased. Yes. Another way to do it is to change the class attributes of elements with setAttribute. Then you can organize the styles in style sheets rather than packing them all into the elements' style attributes, and just switch the elements' classes so they get selected by different selectors. This kind of thing: .active { border: 2px solid red; background-color: yellow; color: black; etc. .. } contentDiv.setAttribute("class", "active") Yeah, be careful doing this though. Imagine you've got: div id="contentDiv" class="class1 class2 inactive">bhakjfh</div You want to switch between active and inactive and you do: contentDiv.setAttribute("class", "active") and suddenly it's: div id="contentDiv" class="active">bhakjfh</div Whereas what you really want is: div id="contentDiv" class="class1 class2 active">bhakjfh</div So you probably want to use the split method on contentDiv.className instead to get an array of class names, then replace inactive with active, glue the strings in the array back together and reassign it |
#6
| |||
| |||
|
|
On Apr 27, 6:21 am, "Nik Coughlin" <nrkn.... (AT) gmail (DOT) com> wrote: Yeah, be careful doing this though. Imagine you've got: div id="contentDiv" class="class1 class2 inactive">bhakjfh</div You want to switch between active and inactive and you do: contentDiv.setAttribute("class", "active") and suddenly it's: div id="contentDiv" class="active">bhakjfh</div Whereas what you really want is: div id="contentDiv" class="class1 class2 active">bhakjfh</div So you probably want to use the split method on contentDiv.className instead to get an array of class names, then replace inactive with active, glue the strings in the array back together and reassign it Or do: function activate_item(){ this.className=this.className.replace(/passive/,'active'); // change passive to active } function passivate_item(){ this.className=this.className.replace(/active/,'passive'); // change active to passive } |
#7
| |||
| |||
|
|
Or do: function activate_item(){ this.className=this.className.replace(/passive/,'active'); // change passive to active } function passivate_item(){ this.className=this.className.replace(/active/,'passive'); // change active to passive } While easier to program, the downside to this as a general approach is that you can wind up changing things you didn't mean to change. class="one alone" Replace class "one" with class "two": class="two altwo" which is probably not what was intended. |
#8
| |||
| |||
|
|
"Harlan Messinger" <hmessinger.removet... (AT) comcast (DOT) net> wrote in message news:67osepF2q55i5U2 (AT) mid (DOT) individual.net... Or do: function activate_item(){ this.className=this.className.replace(/passive/,'active'); // change passive to active } function passivate_item(){ this.className=this.className.replace(/active/,'passive'); // change active to passive } While easier to program, the downside to this as a general approach is that you can wind up changing things you didn't mean to change. class="one alone" Replace class "one" with class "two": class="two altwo" which is probably not what was intended. Yep but you can match regular expressions against whole words only which would avoid that issue |
#9
| |||
| |||
|
| It's probably best to use reusable code for operations like this, and fortunately all of the major JS frameworks include this. The following URL provides a good summary of this: http://www.openjs.com/scripts/dom/cl...nipulation.php |
#10
| |||
| |||
|
|
On Apr 27, 2:20 pm, "david.karr" <davidmichaelk... (AT) gmail (DOT) com> wrote: It's probably best to use reusable code for operations like this, and fortunately all of the major JS frameworks include this. The following URL provides a good summary of this: http://www.openjs.com/scripts/dom/cl...nipulation.php |
|
remove "baz -> "foo quux". The add |
|
Cool, thanks! I'd actually managed to google up JQuery but I wasn't sure about JavaScript "libraries" and all that.... |
![]() |
| Thread Tools | |
| Display Modes | |
| |