Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

234
Vistas
How to use onclick event in JavaScript?

I'm trying to make a window that slide up when the X button(close.png) is clicked.

I added the Wrap element with JavaScript, and added an img element inside.

Then, I put following JavaScript, but there is no change when I press the X button.

<script>
    const parent3 = document.querySelector('#wrap');
    const billingField3 = document.querySelector('#woocommerce-input-wrapper');

    const newImg = document.createElement('img');
    newImg.setAttribute("src", "//t1.daumcdn.net/postcode/resource/images/close.png");
    newImg.setAttribute('id', 'btnFoldWrap');
    newImg.style.cssText = 'cursor:pointer;position:absolute;right:0px;top:-1px;z-index:1';
    newImg.onclick = "offDaumZipAddress();"
    parent3.insertBefore(newImg, billingField3);
</script>


function offDaumZipAddress() {
        jQuery("#wrap").slideUp();
    }

Website structure is

<div class="woocommerce-billing-fields__field-wrapper">
   <p class="billing_postcode_find_field">..
       <span class="woocommerce-input-wrapper">...
       </span>
     </p>
  
   <div id="wrap" ..> 
        <img src="..."></img>
     </div> 
   
  <p class="billing_address_1_field">
        <span class="woocommerce-input-wrapper">

Checking with the console of chrome developer tools doesn't show any errors.

Could someone please let me know what am I missing?

Thank you.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The value of the onclick property must be a function reference, not a JavaScript string.

newImg.onclick = offDaumZipAddress;
about 4 years ago · Juan Pablo Isaza Denunciar

0

You have your answer; here is a working example of that loosely based on your code (so the inserted image actually shows, added some CSS etc. to illustrate)

//gets first one of this type
const billingField3 = document.querySelector('.woocommerce-input-wrapper');
// Get a reference to the parent node/ gets first one of this type
const parent3 = billingField3.parentNode;
//console.log(parent3);
//console.log(billingField3);
// Create the new node to insert
const newImg = document.createElement('img');
newImg.setAttribute("src", "//t1.daumcdn.net/postcode/resource/images/close.png");
newImg.setAttribute('id', 'btnFoldWrap');
newImg.setAttribute('alt', 'folderWrap');
// no not this: newImg.style.cssText = 'cursor:pointer;position:absolute;right:0px;top:-1px;z-index:1';
// this:
newImg.classList.add("inserted-image");
newImg.onclick = offDaumZipAddress;
//console.log("Thing:",newImg);

//console.log("HTML:", parent3.innerHTML);
parent3.insertBefore(newImg, billingField3);
//console.log("New HTML:", parent3.innerHTML);

function offDaumZipAddress() {
  console.log('here we go');
  jQuery("#wrap").slideUp();
}
.billing_postcode_find_field {
  border: solid blue 1px;
  padding: 1rem;
}

.woocommerce-input-wrapper {
  border: solid 1px lime;
  padding: 1rem;
}

.inserted-image {
  cursor: pointer;
/* This is odd, makes it not clickable:
position: absolute;
  right: 0px;
  top: -1px;
  z-index: 1;*/
  border: solid 1px red;
  min-width: 1.5rem;
  min-height: 1.5rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="woocommerce-billing-fields__field-wrapper">
  <p class="billing_postcode_find_field">..
    <span class="woocommerce-input-wrapper">...</span>
  </p>

  <div id="wrap">
    <img src="//t1.daumcdn.net/postcode/resource/images/close.png" alt="png"></img>
  </div>

  <p class="billing_address_1_field">
    <span class="woocommerce-input-wrapper"></span>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda