Change Button Class with Javascript of sorts - Stack Overflow

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

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

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

Share Improve this question asked Sep 11, 2013 at 3:59 user2759977user2759977 271 gold badge2 silver badges8 bronze badges 1
  • jsfiddle/akwZf/4 – Dipesh Parmar Commented Sep 11, 2013 at 4:11
Add a ment  | 

2 Answers 2

Reset to default 2

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745409262a4626454.html

相关推荐

  • Change Button Class with Javascript of sorts - Stack Overflow

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

    5小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信