Jump to content
  • 0

Allow User To Control Image Downloads


RonAnderson

Question

The code below was given by CoopperBackPack and it's working really well and I was very grateful for his help. I'd like to go one step further by giving the user the option to make the image download or not by including a Virtual field (Type is Checked and the default is False) so that if the checkbox is ticked the Image will be downloaded and if it's unchecked the download will not happen. I've tried amending the footer code to execute the download if it's checked. The new line is in bold and underlined:

Footer:
<script>
document.addEventListener('DataPageReady', function (event) {
document.querySelector('input[id*="cbParamVirtual12"]').addEventListener("change", imageHandler); 
   
if(document.querySelector("input[name*="cbParamVirtual13"]").checked == true){

      function imageHandler(event) {
          let calcField = event.target.value;
          document.querySelector("#item-img").src= calcField;

          document.removeEventListener('DataPageReady', imageHandler);
     }        
});
</script>

I've also tried the line in the HTML block but I just can't get it to work. The HTML Block code is below

HTML Block:
<img id="item-img" width="680px" </img>

Could someone please help?

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

It looks like there might be a small syntax error in the provided code. Here's a corrected version of the JavaScript code in the footer:

```javascript
<script>
document.addEventListener('DataPageReady', function (event) {
  document.querySelector('input[id*="cbParamVirtual12"]').addEventListener("change", imageHandler);

  function imageHandler(event) {
    let calcField = event.target.value;
    let downloadCheckbox = document.querySelector("input[name*='cbParamVirtual13']");
    
    if (downloadCheckbox.checked) {
      document.querySelector("#item-img").src = calcField;

      // Add your download logic here
      // For example, window.location.href = calcField; to initiate the download
    }

    document.removeEventListener('DataPageReady', imageHandler);
  }
});
</script>
```

This code checks if the checkbox with the name containing "cbParamVirtual13" is checked before setting the image source and performing any download-related actions. If the checkbox is checked, it updates the image source and executes the download logic. Adjust the download logic according to your requirements.

Make sure to check for any additional errors or issues in the rest of your code that might be affecting its functionality.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...