Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to output descending number in html textbox?

HTML code

  <form id = "numbersdescendingodd">
       Resultdescendingodd : <input type = "text" name = "resultdescendingodd" id = "dodd" max = "20"><br>
       <input type = "button" onclick = "descendingodd()" value = "Clickhere">
       <br>
  </form>

Javascript code

function descendingodd(){
    var x = 0
    for(var i = 21 ; i>=1; i-=2){
        var result = document.getElementById('dodd');
        result.value = (`${i}`)
     }
 }

i want my output textbox are : 21,19,17,15,etc

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are setting the textbox’s value to each descending odd number one at a time and so once your code finishes running it will be left set to the last number (1).

Try changing result.value = (${i}) to result.value += i + ','. It won’t do exactly what you want (it’ll have a comma after the last number) but I’m guessing you’ll be able to figure it out from there.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading