jquery - Remove and Add class on load based on element id in javascript -


this question has answer here:

when select 2 buttons page reloads , there 2 values assigned variables. trying set when page loads removes class , adds 1 based on value of 2 variables is.

i variables pulling value passed 2 hidden input fields , id's

<input id="notype" name="notype" type="hidden" value="1" /> <input id="noassign" name="noassign" type="hidden" value="0" /> 

i grab values on load. have buttons ids type1 type2 type3 add , remove classes from.

$(document).ready(function () {     var type = document.getelementbyid("notype").value;     var assign = document.getelementbyid("noassign").value;     $("type" + type).removeclass("btn-xxx")            .addclass("btn-success");     $("assigned" + assign).removeclass("btn-xxx")            .addclass("btn-success");  }); 

i 100% sure i'm grabbing correct ids off of getelementbyid hidden inputs , attaching using them when trying reference id of buttons. classes not being added or removed. missing?

these buttons

<button id="type1" class="btn-t btn-xxx type-button"  onclick="settype(1);"><i class="fa fa-info-circle fa-fw"> </i>invitation</button><button id="type2" class="btn-t btn-xxx type-button"  onclick="settype(2);"><i class="fa fa-info-circle fa-fw"> </i>assistance</button><button id="type3" class="btn-t btn-xxx type-button"  onclick="settype(3);"> <i class="fa fa-info-circle fa-fw"> </i>finalize</button> 

as removeclass() , addclass() jquery functions.

the error correct document.getelementbyid("type" + type) returns dom element , don't have above methods. same operations can performed using id selector (“#id”)

selects single element given id attribute.

use

$("#type" + type).removeclass("btn-xxx").addclass("btn-success"); $("#assigned" + assign).removeclass("btn-xxx").addclass("btn-success"); 

Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -