javascript - JQuery is adding a class but not removing it from multiple td elements -


i'm trying make more 1 <td> field visible or invisible on screen based on user input , element class. i'm using jquery, html, , javascript.

basically, have html table different fields. want these fields have class of either "visible", "visible , required", or "invisible". when user selects option, changes class of element removing previous 1 , adding new one. onload default of these fields should invisible.

html:

<body onload="showthescreen()">  <table border="1">  <tr>   <td class="reasoncode" align="center">    <span class="fieldlabel">reason code</span>   </td>   <td class="costcenter" align="center">    <span class="fieldlabel">cost center</span>   </td>  </tr>  <tr>   <td class="reasoncode" align="center">    <input type="text" id="reasoncode" name="reasoncode" value="1243">   </td>   <td class="costcenter" align="center">    <input type="text" id="costcenter" name="costcenter" value="00123">   </td>  </tr> </table>  <input type="button" value="makevis" onclick="populatetable()">  </body> 

javascript:

function showthescreen(){   togglefieldvisibility(".reasoncode", 3);   togglefieldvisibility(".costcenter", 3);   displayfields(); }  function populatetable(){   togglefieldvisibility(".reasoncode", 2);   togglefieldvisibility(".costcenter", 1);   displayfields(); }  function togglefieldvisibility(element, x){  switch(x){   case 1:    $(element).removeclass("invisible required").addclass("visible");    break;   case 2:    $(element).removeclass("invisible").addclass("visible required");    break;   case 3:    $(element).removeclass("visible required").addclass("invisible");    break;   default:    $(element).removeclass("visible required").addclass("invisible");    break;  }  displayfields(); }  function displayfields(){  $(".invisible").css({"visibility":"hidden"});  $(".visible").css({"visibility":"visible"}); } 

the problem i'm having this: when open page, fields "invisible" class added them should , become hidden. when try , remove invisible class later , add visible class, invisible class never removed , visible class never added: elements retain classes had @ first, , therefore stay hidden.

i saw previous threads relating problems jquery add/removeclass, none seemed me out. let me know if need more info.

update 1: replies, everyone! unfortunately, , thought happen, code posted here far simplified version of code have , of answers i've received seem related syntax posted--like issue quotes. i've updated code better reflect i'm doing. narrow down issue is.

update 2: know doing wrong. in code have shown here i'm calling togglefieldvisibility(".reasoncode", 2), works fine. in actual code, retrieving number 2 sql call using outside application, , returning string. "2" compared 2 in switch (or "1" 1 , "3" 3) , go default, that's why fields came invisible. hah!

i think problem lies in inline onclick handlers. have them, using quotation marks both attribute delimiters , wrap strings; going cause attribute truncated togglefieldvisibility(" , function not run.

try:

<input type="button" value="makevis" onclick="togglefieldvisibility('.reasoncode', 1)"> <input type="button" value="makevisreq" onclick="togglefieldvisibility('.reasoncode', 2)"> <input type="button" value="makeinvis" onclick="togglefieldvisibility('.reasoncode', 3)"> 

if need use "s reason, can escape them \: (\".reasoncode\"...


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -