0

I have a little piece of code like this:

<div class="content">
    <form id="frmButtons">
        <div id="datagrid">
            <div id="wrapper">
                <div class="ColVis">
                    <button class="ColVis_Button ColVis_MasterButton ui-button ui-state-default">
                        <span>Change Columns</span>
                    </button>
                </div>
            </div>
        </div>
    </form>
</div>

My button does not have an ID or name. How do I select and replace the "class" attribute of that button? I am trying this, but it doesn't work.

$("div.Colvis button").toggleClass("newButtonClass");
5
  • Is that button triggering a post back since it's in a form? Commented May 9, 2014 at 18:19
  • nope. It just shows or hides a div Commented May 9, 2014 at 18:21
  • can you provide the css classes for completeness? Commented May 9, 2014 at 18:22
  • Why? I just need to replace the CSS class "ColVis_Button ColVis_MasterButton ui-button ui-state-default" with a class called "newButtonClass" for the button in my form using JQuery Commented May 9, 2014 at 18:24
  • 1
    @Jake for completeness so that anyone else attempting to perhaps hide a div or button would also benefit from this question as it stands this is just a typo issue. Commented May 9, 2014 at 18:32

4 Answers 4

2

If you want to completely change the class of the button:

$("div.ColVis > button").prop('class', 'newButtonClass');

If you want to add a new class while retaining the current classes:

$("div.ColVis > button").addClass('newButtonClass');

The > looks for children of the first selector.

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

1 Comment

Thanks ! I just need to replace the entire class. Your solution works great ! :)
2

I believe that your code won't work merely because you have a typo in the class name "ColVis" (instead of "Colvis"). It's case-sensitive.

Comments

0

toggleClass only affects the class you pass in, in your case it only adds newButtonClass - If you want to toggle all the other classes off, include them as an argument:

$("div.Colvis button").toggleClass("newButtonClass ColVis_Button ColVis_MasterButton ui-button ui-state-default");

If the class is present at the time of this call, it's removed, and if not present, then it's added.

Comments

0

jsfiddle

Description

First I added an event binding so that when the button is clicked something happens.

$("div .ColVis button").click(function(e){

Next we needed to toggle the class:

$(this).toggleClass("newButtonClass");

Since we are already in an event of the item we are wanting this keyword literally means the button element.

html

<div class="content">
    <form>
        <div id="datagrid">
            <div id="wrapper">
                <div class="ColVis">
                    <button class="ColVis_Button ColVis_MasterButton ui-button ui-state-default">
                        <span>Change Columns</span>
                    </button>
                </div>
            </div>
        </div>
    </form>
</div>

jQuery:

$("div .ColVis button").click(function(e){
    $(this).toggleClass("newButtonClass");
    //This line is to show it works in the console
    console.log($(this).attr('class'));
});

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.