Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

111
Visualizações
Simple Javascript is not running within my HTML

I am new to coding so sorry if there is a blatant mistake I am missing. I made sure both files are in the same folder. I linked the js file to the HTML file using the correct path. I made sure my browser has JS enabled for websites. The code works in fiddle but does not work locally.

Java script

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">

<html>
<body>

<script  type="text/javascript" src="app.js" charset="utf-8"></script>

<select id="brands">
<option value="trane">trane</option>
<option value="brand2">brand2</option>
<option value="brand3">brand3</option>
</select>
    
<input id="txtField" type="text" name="b">
<button onClick="check();">submit</button>
<div id="result"></div>

</body>
</html>

HTML

var brands = {
    "trane": tonnageTrane,
    "brand2": {},
    "brand3": {}
  }

var tonnageTrane = {
    "18": 1,
    "24": 2,
    "30": 2.5,
    "36": 3,
    "42": 3.5,
    "48": 4
  }
  

  function check() {
    var select = document.getElementById('brands');
    var value = select.options[select.selectedIndex].value;
    var inp = document.getElementById('txtField').value;
    
    document.getElementById('result').innerHTML = brands[value][inp.substring(4, 6)];
  }

edit: added the text version of code

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You should write your code as below when posting a question or answer.

Your code repeats its HTML tag twice, your script tag should be in the head, and tonnage should be above brands since tonnage is used in it - regardless though your code should work. Your issue is most likely that your JavaScript file is not in the same folder as your HTML file. You can also use the HTML I made from yours below just in case there is a syntax issue I did not see; I tested the code and it works fine. When debugging your JavaScript code, you should use the console of the web browser; it will show you the errors.

JS:

var tonnageTrane = {
    "18": 1,
    "24": 2,
    "30": 2.5,
    "36": 3,
    "42": 3.5,
    "48": 4
}

var brands = {
    "trane": tonnageTrane,
    "brand2": "brand2test",
    "brand3": "brand3test"
}

function check(){
    var select = document.getElementById('brands');
    var value = select.options[select.selectedIndex].value;
    var inp = document.getElementById('txtField').value;
    document.getElementById('result').innerHTML = brands[value][inp.substring(4, 6)];
}

HTML:

<!DOCTYPE html>
<html lang="en">
    <head>
        <script type="text/javascript" src="app.js"></script>
    </head>
    <body>
        <select id="brands">
            <option value="trane">trane</option>
            <option value="brand2">brand2</option>
            <option value="brand3">brand3</option>
        </select>
        <input id="txtField" type="text" name="b">
        <button onClick="check();">submit</button>
        <div id="result"></div>
    </body>
</html>
about 4 years ago · Juan Pablo Isaza Relatório

0

You have a few problems. First you have two opening <HTML> tags. Next, you are missing a <head> tag.

As for your script, you either need to link to the JS file LAST inside of the body tag, like this:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
 </head>
  <body>

    Your HTML Here

    <script type="text/javascript" src="app.js" charset="utf-8"></script>
  </body>
</html>

Or within the head tag, like this:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <script type="text/javascript" src="app.js" charset="utf-8"></script>
  </head>
  <body>
    Your HTML Here
  </body>
</html>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda