skip to Main Content

This is my code

function updateName(nombreCampo, valor) {
    var campo = document.getElementsByName(nombreCampo)[0];
    campo.value = valor;
    campo.dispatchEvent(new Event('input', { bubbles: true }));
  }

  function updateId(nombreCampo, valor) {
    var campo = document.getElementById(nombreCampo);
    campo.value = valor;
    campo.dispatchEvent(new Event('input', { bubbles: true }));
  }

function updateFormData(data) {
    var listContent = data.TablaContenidos.split(", ").join("n");
    updateName('dc.title', data.Titulo);
    updateName('dc.contributor.author', data.Autor);
    updateId('dc_date_issued_year', data.Ano);
    updateName('dc.identifier.citation', data.Citacion);
    updateName('dc.publisher', data.Publisher);
    updateName('dc.language.iso', data.Idioma);
    updateName('dc.subject.proposal', data.Palabras);
    updateName('dc.subject.ddc', data.ClasificacionDewey);
    updateName('dc.description.abstract', data.Resumen);
    updateName('dc.description', data.DescripcionRecurso);
    updateName('dc.description.tableofcontents', listContent);
    updateName('dc.relation.references', data.ReferenciasBibliograficas);
  floatingButton.innerHTML = 'Cargados - Reintentar';
  console.log('Datos actualizados.');
}

It works very well, but when I send the form the values are empty. The problem is that I only can use Javascript I can’t modify the actual form logic or the html. And I can’t use the form ID for some reasons.

I hope to set the values ​​with Javascript as if I manually put the values ​​in the inputs

2

Answers


  1. Chosen as BEST ANSWER

    I have found the solution

    function updateName(nombreCampo, valor) {
    var campo = document.getElementsByName(nombreCampo)[0];
    campo.value = valor;
    campo.dispatchEvent(new Event('input', { bubbles: true }));
    campo.dispatchEvent(new Event('change', { bubbles: true }));}
    

    Adding campo.dispatchEvent(new Event('change', { bubbles: true })) The value have been set as If I write it manually


  2. Here is my previous code.

    document.querySelector("form").addEventListener('formdata', (e) => {
    
      const formData = e.originalEvent.formData; 
      
      formData.append('alphabet', "abcde");
      formData.append('a1b2', "123123");
    });
    

    I think it might be helpful for you.
    Good luck

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search