Forum Moderators: open
JavaScript
// Width Change
function changewidth(id, newWidth)
{
identity=document.getElementById(id);
identity.style.width=newWidth;
}
(X)HTML
<element onclick="changewidth('content', '100%');"
It makes sense to also allow which CSS property would be choosen in addition to the value. So this is what we feed to the script...
1.) Element's ID
2.) The CSS property (could be width, border, etc).
3.) The value for the CSS property (100%, red, underline, etc).
What I'm not sure of is how to assign all this together. I don't have any good working examples of that. Here is what I have thus far...
JavaScript
// Dynamic CSSJS Change
function changecss(id, cssproperty, cssvalue)
{
identity=document.getElementById(id);
identity.style=cssproperty;
identity.style.value=cssvalue;
}
(X)HTML
<element onevent="changecss('contentID', 'CSSProperty', 'CSSValue');"
I think that should make sense but I know how I'm associating the dynamic style is wrong.
JavaScript
// setStyleById: given an element id, style property and
// value, apply the style.
// args:
// i - element id
// p - property
// v - value
//
function setStyleById(i, p, v) {
var n = document.getElementById(i);
n.style[p] = v;
}
(X)HTML
<element onevent="setStyleById('content', 'width', '100%');"
- John
Consider adding to your code the use of 'this' so that you don't need to set an id and use getElementById(). 'this' will automatically refer to whatever element fired the event.
function setStyle(p, v) {
this.style[p] = v;
}