0

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.

4
  • If you're using jQuery, why don't you use jQuery event binding, instead of assigning to .onclick? Commented Feb 16, 2022 at 22:40
  • because function offDaumZipAddress() is out of <script> ..... </script> Commented Feb 16, 2022 at 22:41
  • Hi, @Barmar. I'm new to JavaScript, so I don't know about jQuery event binding 🤣 . I saw a window slide using that Jquery in another tutorial and tested it on my website and it worked well. So I try to apply this in a different way, but it doesn't work. Commented Feb 16, 2022 at 22:44
  • Hi, @MisterJojo I tried put that function into script, but still not working. Commented Feb 16, 2022 at 22:46

2 Answers 2

1

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

newImg.onclick = offDaumZipAddress;
Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.