Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
Cómo agregar el texto HTML adyacente solo una vez onblur

Estoy tratando de validar mi formulario de pago de woocommerce en la interfaz solo con javascript. La validación se ejecuta correctamente, pero el problema es que se llama a la función cada vez que elimino el foco del campo de entrada (he usado el evento onblur). El div se crea varias veces. Quiero que se muestre solo una vez, de lo contrario, debería eliminarse. ¿¿Puede alguien sugerir una solución?? Adjuntando el código y SS a continuación.

 <h3>my custom heading</h3> <p class="form-row form-row validate-required"> <input type="billing_first_name">... </p> <p class="sp-error-notice" id="sp_billing_first_name_field"> </p>
 function myFunction() { // for name let n = document.getElementById("billing_first_name").value; // let name_text; if ( n.length < 3 || n == '' ) { var n1 = document.getElementById('billing_first_name'); n1.insertAdjacentHTML('afterend', '<p class="sp-error-notice" id="sp_billing_first_name_field">This is a test.</p>'); } else { var n1 = document.getElementById("sp_billing_first_name_field"); n1.remove(); } }

La etiqueta P se crea varias veces

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Simplemente puede verificar la existencia de la etiqueta p antes de agregarla

 if(!document.getElementById("sp_billing_first_name_field")){ n1.insertAdjacentHTML('afterend', '<p class="sp-error-notice" id="sp_billing_first_name_field"></p>'); }
about 4 years ago · Juan Pablo Isaza Report

0

No es necesario poner else , porque el elemento <p class="sp-error-notice" id="sp_billing_first_name_field">This is a test.</p> nunca se creó, no se puede remove() en otro lugar condición. Además, no hay necesidad de poner n == '' en la condición que su primera condición es suficiente y satisface n == '' .

Aquí lo que hice fue verificar si el elemento fue creado, si ya existe, regresa, lo que provoca la repetición.

 function myFunction() { // for name let n = document.getElementById("billing_first_name").value; // let name_text; if(document.getElementById("billing_first_name").nextSibling.nodeName == 'P'){ return } if ( n.length < 3 ) { var n1 = document.getElementById('billing_first_name'); n1.insertAdjacentHTML('afterend', '<p class="sp-error-notice" id="sp_billing_first_name_field">Error.</p>'); } } myFunction() myFunction() myFunction() function myFunction2() { if(document.getElementById('sp_billing_first_name_field')){ document.getElementById('sp_billing_first_name_field').style.display = 'none' } }
 <input type="text" id="billing_first_name" onclick='myFunction2()'>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!