PDA

View Full Version : Changing an attribute.



droide
07-13-2006, 03:18 AM
Hi,

I really need help with this please, could anyone show me how I can change an attribute to a tag dynamically (like the "domroll script" for img but for example: class).

this is what I want to do:

I am using the iFrame SSI Script (http://www.dynamicdrive.com/dynamicindex17/iframessi2.htm) to display data, and what I want to do is that when I click on the link (image) that's pointing to the Iframe and I want to change the "class" atribute of it and another img's classe on the same page, so there is 2 or more attribute changing in the same page.

Could someone please help, and sorry for the english.
Sincerily,
Josť

jscheuer1
07-13-2006, 03:57 AM
The way to change the class of an element is like so:


element.className='newClass'

You can get the element in any valid way, the most common being its id, via getElementById('id') but, if it is an image, its name will do.

droide
07-13-2006, 04:10 AM
Thanks,

I will try that...

droide
07-13-2006, 04:12 AM
Sorry,

I'm not a programmer actually, I'm more a designer how sould I write this, a function, an if?

Please...I'm sorry again

jscheuer1
07-13-2006, 04:31 AM
OK, lets say you have two classes defined in a stylesheet like so:


.stlye1 {
color:red;
}

.style2 {
color:blue;
}

and you want to toggle them based upon which is active upon click for a given element:


<span id="tog" class="style1" onclick="toggle();">Hi there! Click me!</span>

Then you could have this function in a script block in the head:


function toggle(){
var el=document.getElementById('tog');
if (el.className=='style1')
el.className='style2';
else
el.className='style1';
}

If you wanted to include another element:


function toggle(){
var el=document.getElementById('tog');
var el2=document.getElementById('tog2');
if (el.className=='style1')
el.className=el2.className='style2';
else
el.className=el2.className='style1';
}

There are many ways to work this out and the best/most appropriate method would depend upon your markup and exactly what you want to have happen but, that's the basic idea.

droide
07-13-2006, 03:43 PM
Thank you so much, really appreciated.

I'll work around with that...

Josť