0

I'm trying to modify the input type for several hidden fields on the following page when the page loads, but in using the following jquery to modify one of the currently hidden fields, but no updates are taking effect:

 $jQ('[name="FirstName"]').prop('type', 'text');

Can you please recommend an alternative approach. Thanks very much.

You can find the page here:

http://www3.sungevity.com/Modernize.html?

Franco
  • 2,309
  • 1
  • 11
  • 18
DMandeezy
  • 1
  • 1
  • 2

3 Answers3

0

You wrote $jQ(...) that either has to be jQuery(...) or $(...)

Johannes
  • 64,305
  • 18
  • 73
  • 130
  • Thanks, @Johannes. I moved this jQuery script to it's own section of the code with only the following, but still not effect on the input type of the First Name field:: – DMandeezy Oct 05 '16 at 04:43
0

Maybe you mean this:

$('input[name="FirstName"]').prop('type', 'text');
Franco
  • 2,309
  • 1
  • 11
  • 18
0

Changing the type of an already rendered HTMLInputElement is a bit of a security and data consistency issue and many browsers (especially IE and old versions of FF) don't allow for this to happen. Which is reasonable.

That said, some browsers (looking at Chrome 83 right now) do allow on-the-fly type changes. Changing between incompatible formats (e.g. password->number) will clear any value. Changing anything to hidden will remove the input element from the document flow. Etc.

If you can find any documentation on what exactly happens when transitioning between various type pairs, please edit the resource into here. Do change events trigger? Does data get lost?

A SOLUTION

The simplest clean solution in my opinion is to create a new input[type="text"] and insert it into the document after the hidden input. Then, add the relevant event listeners (starting at least with keyup and change) onto the visible input to immediately propagate the edited value to the hidden input. Then trigger the same event on the hidden input.

This way, any code that relies on the hidden input will continue working correctly and you will have added UI to proxy the hidden input's value.

Example HTML:

<h1>Hidden Input Proxy</h1>
<input id="original_field" type="hidden" value="this is the initial value" />

Example vanilla JS (add your own error handling):

function proxyHiddenInput(inputSelector) {
    const hidden = document.querySelector(inputSelector);
    const proxy = document.createElement('input');
    proxy.type = 'text';
    proxy.value = hidden.value;
    hidden.after(proxy);

    function propagateValue(event) {
        hidden.value = proxy.value;
        hidden.dispatchEvent(new Event(event.type));
        if (event.type !== 'change') {
            hidden.dispatchEvent(new Event('change'));
        }
    }
    
    proxy.addEventListener('keyup', propagateValue);
    proxy.addEventListener('change', propagateValue);
    proxy.addEventListener('blur', propagateValue);

    return proxy;
}

proxyHiddenInput('#original_field');

You can make this happen a little more tersely with jQuery.

RELATED

Roubo
  • 13
  • 4