Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Client-side Scripting _ why this code is not running

Posted by: aqeel Apr 12 2022, 08:53 AM

<center>
<p></p>
<button class="srd" id="btn"><b>click here to get code </b></button>
<a class="button" href="" id="download"> BR260695 </a>
<script>
var downloadButton = document.getElementById("download");
var counter = 19000;
var newElement = document.createElement("p");
newElement.innerHTML = "";
var id; downloadButton.parentNode.replaceChild(newElement, downloadButton);
function startDownload() { this.style.display = 'none';
id = setInterval(function () { counter--;
if (counter < 0) {newElement.parentNode.replaceChild(downloadButton,newElement);
clearInterval(id);
}
else { newElement.innerHTML = +counter.toString() + "<b>⏱️ WAIT A Second</b>."; } }, 0); };
var clickbtn = document.getElementById("btn");
clickbtn.onclick = startDownload;
</script>
</center>

Posted by: Christian J Apr 12 2022, 10:47 AM

Works for me. What's the error?

Posted by: coothead Feb 8 2023, 09:59 AM

QUOTE(Pervive @ Feb 8 2023, 08:09 AM) *

. Pleased to see your issue as I think I have the same problem,
. I am also confused and in need of light on this same issue.

. Need help.



As https://forums.htmlhelp.com/index.php?showuser=7 has already pointed out that the OP's posted
code works OK, can you explain...
  1. how you have the same problem
  2. what you are confused about
  3. why on earth you need help. IPB Image

coothead


Posted by: pandy Feb 8 2023, 07:33 PM

He/it just posts to inject his spam link later.

Posted by: Jason Knight Feb 11 2023, 12:50 AM

CODE
<center>

What is this, 1997?

CODE
<p></p>

Hoping you deleted the content on purpose for posting on the forum and not just using this as a spacer, since that's not markup's job.

CODE
<button class="srd" id="btn"><b>click here to get code </b></button>

what form is this submitting? You didn't say type="button" and the default for <button> is type="submit"

Also what makes "click here to get some code" a proper name, personal title, or legal entity. Aka what <b> actually means? Don't just use <b> becuase you want the text bold, use it because it should be bold for grammatical reasons when not receivng "more emphasis" (<strong>'s job)

CODE
<a class="button" href="" id="download"> BR260695 </a>

If that's supposed to be a button, why isn't it a button?

CODE

  <script>
    var downloadButton = document.getElementById("download");  
    var counter = 19000;
    var newElement = document.createElement("p");
    newElement.innerHTML = "";  

You literally just created that paragraph. Thus its content is empty.

CODE

    var id;
    downloadButton.parentNode.replaceChild(newElement, downloadButton);
    function startDownload() {
            this.style.display = 'none';

Why would the function "startDownload" be an Element object? Did you meant to use Event.currentTarget?

CODE

            id = setInterval(
                function () {
                    counter--;
                    if (counter < 0) {
                        newElement.parentNode.replaceChild(downloadButton,newElement);  
                        learInterval(id);
                    else {

I think you're missing a "{" there.

CODE

            newElement.innerHTML = +counter.toString() + "<b>?? WAIT A Second</b>.";
         }
      }, 0);
    };  

Which I think that closing bracket finally cropped up here. Either way you shouldn't be generating markup with innerHTML for "bold that's CSS' job" and "plaintext". Hell if you created a textnode for the counter you wouldn't even need to touch the rest of the contents inside the interval.

CODE

    var clickbtn = document.getElementById("btn");  
    clickbtn.onclick = startDownload;  
  </script>

It's an element already on the DOM, use addEventListener not onclick.

CODE

</center>

Did I mention this isn't 1997?

The mix of 25 year out of date code and putting stuff together on single lines that shoudln't be is leading to a lot of errors. Worse, it makes it hard to figure out what it is you want this to even do.

Could you just better explain what you want this to do? Maybe one of us could then provide modern working code... instead of ... that.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)