Unintentional removal of a class from an input?

Hello, all,

Other than expressly writing a script to remove a class from an input element - is there a way to remove a class from an element??

I have a form that I’m using jQuery serializeArray() to send via AJaX to a processing page. The user has an option to save some form information in localStorage, so they don’t have to keep filling it out every time they use the form. Everything is working great, except for one thing. Second and subsequent submits will remove two elements from the localstorage. I’ve tracked it down to these elements being stripped of a class called ls. ls is how I determine which elements are eligible for inclusion into localstorage. SO - the first time the form loads, submit will include these two fields. Subsequent submits, whether the page is reloaded or not, result in these two form fields being stripped from the ls class and (ergo) not included in the localstorage.

Any thoughts on how this could be? I’ll try to provide some pseudo code in the next post.

V/r,

:slight_smile:

javascript

function validateData(formObjId){
  var postURL = "components/public.cfc?method=tiloproc";
  var lsData = {};
  var ctype = "application/json";
  var lastName = "", saFields = "";
  $.each(
      $('#' + formObjId).serializeArray(),
      function(index,field){
        if($('#' + field.name).hasClass('ls')){
          lsData[field.name] = field.value;
            }
        }
  );
  storage.setItem('proc',JSON.stringify(lsData));
  var postData = $('#' + formObjId).serializeArray();
  $.ajax({
       type: "POST",
       url: postURL,
       data: postData
       })
    .done(function(data){
          $('#results').html(data);
          });
}

html

<input type="text" name="NAICScode1" id="NAICScode1" class="ls required naics" maxlength="10" />NAICS
<input type="text" name="FSCcode1" id="FSCcode1" class="ls required fsc" maxlength="4" />FSC
...

<div id="results"></div>

V/r,

:slight_smile:

I’m not sure what you mean by this.

There is .removeClass() which you can use in your promise.

Sorry… something was removing a class from two of my input fields - against my wishes.

I did find the culprit. When the page loads, if there is a localStorage entry for this form, it pre-populates the form. IF there are more than one set of these inputs, it dynamically generates the extras. I forgot to add the ls class to this generation function. To be honest, I completely forgot that it was running that function on page load, which explains why I was scratching my head for so long, over it. Yes, I do feel like a rookie, thanks. :slight_smile:

V/r,

:slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.