Home > front end >  How to use onclick event in JavaScript?
How to use onclick event in JavaScript?

Time:02-17

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 >
   <p >..
       <span >...
       </span>
     </p>
  
   <div id="wrap" ..> 
        <img src="..."></img>
     </div> 
   
  <p >
        <span >

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.

CodePudding user response:

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

newImg.onclick = offDaumZipAddress;

CodePudding user response:

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 >
  <p >..
    <span >...</span>
  </p>

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

  <p >
    <span ></span>
</div>

  • Related