0

So I have this fix HTML structure that I cannot influence. The structure is provided by a tool, where you add selection/input/etc fields via drag and drop.

E.g. a regular textfield gets this HTML code:

<div id="d_001" class="field  text ">
    <label for="f_001" id="l_001">Shortdescription</label>
        <input id="f_001" name="id1" value=""     />
</div>

And a textarea would get this code:

<div id="d_002" class="field  area ">
    <label for="f_002" id="l_002">Description</label>
        <textarea id="f_002" name="id2" cols="45" rows="5" ></textarea>
</div>

I also have a selection list, that based on the selected item shows/hides further information fields. Let's say for example the selection list contains different clothes. When you select "pants", two input fields "length" and "width" are made visible. When you select "bra", two input fields "cup size" and "underbust" are made visible.

So here's the thing: When selecting "pants", it would currently submit the following information to the system:

Short Descritption: Pants
Description: I would like to by black skinny jeans
Length: 32
Width: 32
Cup Size:
Underbust:

From googling a little I found out that disabling the non-selected fields should do the trick. Yet it doesn't. It clearly disables the fields, as they're grey and not editable, but they still get transmitted when sending the form.

Based on the HTML snippets above I tried disabling via

document.getElementsByTagName('input').disabled = true;

--> which disables all "input" input fields, but still submits the label with no content. Even though I am not sure if it doesn't submit the content or if it still submits the empty content.
(yes, this would leave out selection lists, textareas, etc. but this is the secondary issue, solvable by doing the same for all tag types)

Hence I thought maybe I need to disable all elements within the div item, meaning I'd need to also "disable" the labels via

documents.getElementsByTagName('label').disabled = true;

I am not too surprised it doesn't work. But as I can't disable div items as a whole I really don't know what I am missing. Is there anything else that forces the fields to be submitted? I am very grateful for any hints that push me in the right direction.

Edit:

As per request I want to add the exact loop that disables the mentioned input fields:

// list with links to make the items clickable
      <ul id="myUL">
          <li><a id="li1" title="pants" href="javascript:void(0)" onclick="returncontent(this);showpants();">Pants</a></li>
          <li><a id="li2" title="bra" href="javascript:void(0)" onclick="returncontent(this);showbra();">Bra></li>
          <li><a id="li3" title="tshirt" href="javascript:void(0)" onclick="returncontent(this);showtshirt();">T-Shirt</a></li>
          <li><a id="li4" title="socks" href="javascript:void(0)" onclick="returncontent(this);showsocks();">Socks</a></li>
      </ul>

// a var array containing the available class attributes
var elemente = ["pants","bra","tshirt","socks"];

// defining the functions triggered by the list
function showpants(){
    howtocode(0);
}
function showbra(){
    howtocode(1);
}
function showtshirt(){
    howtocode(2);
    wosOSD();
}
function showsocks(){
    howtocode(3);

// have the function do different things depending on the selected list item
function howtocode(value){
    var arrayLength = elemente.length;
    for (var i = 0; i < arrayLength; i++) {
    var classEle = document.getElementsByClassName(elemente[i]);
    if (elemente[i] === elemente[value]){
      changeStyle(elemente[i],classEle,"block");
      setRequired(elemente[i],classEle);
    }
    else{
      changeStyle(elemente[i],classEle,"block");
      disableFields(elemente[i],classEle);
      emptyFields(elemente[i],classEle);
    }

  }
}
function disableFields(ele, classEle){
    for (var n = 0; n < classEle.length; n++) {
        var inputfields3 = classEle[n].getElementsByTagName('input');
        var textfield3 = classEle[n].getElementsByTagName('textarea');
        for (var o = 0; o < inputfields3.length; o++){
            inputfields3[o].disabled = true;
        }
        for (var p = 0; p < textfield3.length; p++){
            textfield3[p].disabled = true;
        }
    }
}

and the other functions do the following:
- changeStyle() [sets the div item to "block" or "none"
- setRequired() [is self explainatory I hope]
- emptyFields() [resets all input fields if the selected item gets changed]

I am aware that it currently doesn't work. I am working on providing a fiddle.

Edit 2:

So I found the following code somewhere in the system JS/jQuery files. Could this have something to do with it? So far I haven't understood yet what it does.

    var data = elements.inject({ }, function(result, element) {
      if (!element.disabled && element.name) {
        key = element.name; value = $(element).getValue();
        if (value != null && (element.type != 'submit' || (!submitted &&
            submit !== false && (!submit || key == submit) && (submitted = true)))) {
          if (key in result) {
            // a key is already present; construct an array of values
            if (!Object.isArray(result[key])) result[key] = [result[key]];
            result[key].push(value);
          }
          else result[key] = value;
        }
      }
      return result;
    });
  • You say it submits that data, but how do you know what it is submitting? Are you sure you're not looking at the results parsed with the disabled fields added? What is the data being sent in its raw format? – Ashley Jan 16 '20 at 12:55
  • 1
    What mechanism are you using to "submit" the data? A [MRE] would be helpful. – Turnip Jan 16 '20 at 13:01
  • @Turnip well that's where it gets though. The thing is that I am talking about a ticket tool, that provides the option the create "webtickets" so that a user can create a ticket via the webform in said tool. The entire transmission mechanism is a black box. I can look up the jQuery code when looking at the code in the browser, but it's literally just countless rows of unformatted code. But I shall try to retrieve some useful information. – tsfiddlesticks Jan 16 '20 at 13:18
  • 1
    _“`document.getElementsByTagName('input').disabled = true;` which disables all "input" input fields”_ - **no, it doesn’t.** `getElementsByTagName` returns a node list, and you just set the `disabled` property of that node list to true - which achieves nothing. You need to loop over the elements contained in this list, and set the disabled attribute for each of those, individually. – 04FS Jan 16 '20 at 13:19
  • @Ashley at this point I feel obligated to mention my JS journey only just begun several weeks ago. And I fear have no idea on how I would find the answers to your questions. Wht do you mean by "raw format"? – tsfiddlesticks Jan 16 '20 at 13:21
  • @04FS true. I forgot to mention that I have a for loop that picks the individual elements from the node list. But as it's a big loop doing multiple things I thought I'd simplify it this way. But as I mentioned in the post: I am fairly certain the loop is doing it's job, as the input fields are grey and not editable. – tsfiddlesticks Jan 16 '20 at 13:23
  • Provide a proper [mre] then, that shows the behavior your are talking about. With code snippets that aren’t even the real thing, we can hardly help you. – 04FS Jan 16 '20 at 13:25
  • Just to be clear - we are talking about a form that gets submitted the “normal” way? Or is this something where the form data gets gathered & prepared for submission by some script as well? – 04FS Jan 16 '20 at 13:27
  • @tsfiddlesticks It's ok, we were all beginners once! Typically when someone submits a form it is either sent the "normal" way as another user mentioned (https://developer.mozilla.org/en-US/docs/Learn/Forms/Sending_and_retrieving_form_data) or the data is captured, processed, and submitted somewhere a custom way. It's my suspicion that if this were the "normal" way your form would be working as expected and the disabled fields would not be sent. If the data is being sent a custom way and is a "black box" as you say you may not be able to alter its behaviour. – Ashley Jan 16 '20 at 13:34
  • @04FS well I didn't include it because I felt it was unnecessary information. But I included a shortened version of what I did, adapted to the example I used in the description. – tsfiddlesticks Jan 16 '20 at 13:46
  • I am not able to create something that actually runs out of these scattered snippets, when I try to copy&paste them over into a jsfiddle. Please provide something where we can look at this actually live & running, without having it throw errors because stuff is missing in different places. – 04FS Jan 16 '20 at 13:51
  • @Ashley ok gotcha. Seems sorta obvious! Does the following information help at all? I'll try to find some more useful info...
    – tsfiddlesticks Jan 16 '20 at 14:22
  • @tsfiddlesticks So it looks like your form may be submitting the "normal" way. You can use something like Chrome's developer tools to capture the raw data that is being posted by your form. I suggest you do that to see what is actually being sent. Here's a resourse how: https://datawookie.netlify.com/blog/2016/09/view-post-data-using-chrome-developer-tools/ – Ashley Jan 16 '20 at 14:27
  • @Ashley so I am working on getting some info on the raw data. But I kinda need to understand the tool suggestion first. In the meantime I did find some code snippet in the system files and added them to the original post as "Edit 2". Could this be related? – tsfiddlesticks Jan 16 '20 at 16:17

1 Answers1

-3

Take a look at this question, How to disable an input type=text?

Instead of setting disabled = true you need to set disabled = "disabled".

schu34
  • 965
  • 7
  • 25
  • I think you're confusing setting it in the html vs via js. If you look at the non-accepted answer (which has a higher score) you'll see that setting is as `true` via js is correct. However, the poster is saying that even when the input is clearly disabled the field is still being sent which is incorrect behaviour. – Ashley Jan 16 '20 at 13:08