Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
onclick/Button no funciona en Javascript/HTML

No recibí un error o algo así, el botón simplemente no está haciendo la función asignada. Aquí está mi código: Html:

 <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="projectscss.css"> <script src="projectsjs.js"></script> </head> <body> <h1 class="title">BMI Calculator</h1> <button type="button" class="height" onclick="cmtosomethings">Centimetre to Metre/Feet/Kilometres</button> <br> <p>Write 'M' for metres, "KM" for kilometres, "F" for feet, "I" for inches</p> </body> </html>

El código del archivo "projectsjs.js":

 function cmtosomethings() { var op = prompt("Metres, Feet, inches or kilometres?") if (op == "M") { var cmtom = prompt("Write number in centimetre for converting to metres: ") var number = parseFloat(cmtom) var result = number / 30.48 var alert = alert(result) } }

Aquí está el código del archivo "projectscss.css":

 html, body { margin: 0; padding: 0; background-color: chocolate } .title { font-family: "Papyrus", fantasy }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Debe usar onclick="cmtosomethings()" . La expresión se evaluará como javascript.

Consulte también: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_%E2%80%94_dont_use_these

...el valor del atributo es literalmente el código JavaScript que desea ejecutar cuando ocurra el evento...

about 4 years ago · Juan Pablo Isaza Report

0

Por lo general , no es una buena idea usar controladores de eventos en línea. Asigne el controlador dentro del script.

 document.querySelector(`button.height`).addEventListener(`click`, cmtosomethings); function cmtosomethings() { var op = prompt("[M]etres, [F]eet, [I]nches or Kilometres (KM)?"); if (op == "M") { var cmtom = prompt("Write number in centimetre for converting to metres: "); console.log(parseFloat(cmtom) / 30.48); } }
 html, body { margin: 0; padding: 0; background-color: chocolate; } .title { font-family: "Papyrus", fantasy; }
 <h1 class="title">BMI Calculator</h1> <button type="button" class="height"> Centimetre to Metre/Feet/Kilometres </button> <p>Write 'M' for metres, "KM" for kilometres, "F" for feet, "I" for inches</p>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!