1

So, I am trying to change the class for a input box using javascript and can't seem to get it right.

I have 2 CSS classes.

The normal class is .butz and the one I want the button to change to when its clicked is .butzz

So, I have this for my html:

<input id="butval1" class="butz" type="button" name="design1" value="Choose Design" onclick="" />

and this is my js

document.getElementById("butval1").className += "butzz";

What I really want to do, is change the class from butz to butzz or... if possible, change the background color of my button with getElementByClassName

I have 9 buttons that are all in the same class, and I want the one clicked, to change to green or #24f000

thank you all

1

2 Answers 2

3

using javascript you can change the class name using

document.getElementById('butval1').className = 'butzz'

if you want to add a new class by javascript use

document.getElementById('butval1').className += ' butzz'

for change clicked DOM class you need to create function and pass that clicked DOM into function as argument and using that passed args you can do this.

Example

<input id="butval1" class="butz" type="button" name="design1" value="Choose Design" onclick="changeID(this);" />
<input id="butval2" class="butz" type="button" name="design1" value="Choose Design" onclick="changeID(this);" />
<input id="butval3" class="butz" type="button" name="design1" value="Choose Design" onclick="changeID(this);" />

function changeID(args)
{
    args.className = 'butzz'
}

Live Demo

Sign up to request clarification or add additional context in comments.

Comments

1

Try:

<input id="butval1" class="butz" type="button" name="design1" value="Choose Design" onclick="changeID(this);" />
<input id="butval2" class="butz" type="button" name="design1" value="Choose Design" onclick="changeID(this);" />
<input id="butval3" class="butz" type="button" name="design1" value="Choose Design" onclick="changeID(this);" />

function changeID(elm){
    var NAME = elm;
    var currentClass = NAME.className;
    if (currentClass == "butz") { 
        NAME.className = "butzz";   
    } else {
        NAME.className = "butz";  
    }
}

DEMO FIDDLE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.