Search code examples
javascriptfunctionjspdisabled-input

Disable many fields by name javascript


This is my question:

I got an jsp page, this jsp has many text fields like this:

<html:text property="cicPF" maxlength="9" style="text-transform: uppercase;" onfocus="disableIfeFields()"/>

So I want to disable some of this text fields when the focus it's in a specific field

But no one of this fields has "id" label, and I can't modify it to include it.

May I disable the fields usig their given names, no one of this repeats the same name.

for example with a function like this:

function disableIfeFields(){
    document.getElementsByName("numIdentificacionPF").disabled = true;

}

thanks


Solution

  • You need to loop through the list and disable all the fields you want that way, used input to show example:

    function disableIfeFields() {
      document.getElementsByName("numIdentificacionPF").forEach((e) => {
        e.disabled = true;
      });
    }
    <html:text property="cicPF" maxlength="9" style="text-transform: uppercase;" />
    <input onfocus="disableIfeFields()" type="text" name="fname">
    <input type="text" name="numIdentificacionPF">
    <input type="text" name="numIdentificacionPF">
    <input type="text" name="numIdentificacionPF">
    <input type="text" name="numIdentificacionPF">
    <input type="text" name="numIdentificacionPF">