0

I'm making a basic program where the user is shown a current room temperature along with a text field and "Set" button so that they can set their desired room temperature. What I want to happen is when the user enters a number into the text field and hits the Set button it changes the "roomTemp" variable to their desired temperature also known as "desiredTemp" and displays it as the current temperature. I don't think it's reaching the save() function as nothing is printed in my console when the button is clicked when it should output "Temperature is changing!" to my console.

Javascript File:

var http = require('http');
var ejs = require('ejs');
var fs = require('fs');
var roomTemp = 20;
var desiredTemp = 0;

http.createServer(function(req,res) {
  res.writeHead(200, {'Content-Type': 'text/html'});

  //since we are in a request handler function
  //we're using readFile instead of readFileSync
  fs.readFile('index.html', 'utf-8', function(err, content) {
    if (err) {
      res.end('error occurred');
      return;
    }

    var renderedHtml = ejs.render(content, {roomTemp: roomTemp});  //get redered HTML code
    res.end(renderedHtml);
  });
}).listen(3000, "127.0.0.1");
console.log('Server Running at http://127.0.0.1:3000  CNTL-C to quit');

HTML File:

<!DOCTYPE html>
<html>
<head>
<script src="Thermostat.js"></script>
</head>
<body>

Current Temp: <%= roomTemp %> <br></br>

<form>
Desired Room Temperature: <input type="number" id="desTemp" name="roomTempDes"><br></br>
<button onclick="save(document.getElementById("roomTempDes").value)">Set</button>
</form>

<script>
function save(desiredTemp) {
      roomTemp = desiredTemp;
      console.log("Temperature is changing!");
}
</script>

</body>
</html>
8
  • FWIW, you may want to look into something like KnockoutJS/AngularJS and use a bi-directional model. Then it's simply a matter of assigning to model.temperature and the UI changes. Commented Oct 21, 2014 at 13:12
  • Here's an example of KnockoutJS: jsfiddle.net/Lbnzpyjt Check your console to see the update event trigger as you change the temperature. Commented Oct 21, 2014 at 13:20
  • Okay that could work but it has to change with the button click, not as the textfield value changes. I'll see if I can get that to work! Commented Oct 21, 2014 at 13:22
  • jsfiddle.net/Lbnzpyjt/1 Commented Oct 21, 2014 at 13:27
  • I'm using node.js to run the server, I believe I have everything ready to go but it's giving me an error module.js:340 throw err; ^ Error: Cannot find module 'build/output/knockout-latest.debug.js' Commented Oct 21, 2014 at 13:43

2 Answers 2

2

You have too many quotation marks in your script.

"save(document.getElementById("roomTempDes").value)"

should be

"save(document.getElementById('roomTempDes').value)"
Sign up to request clarification or add additional context in comments.

Comments

0

Your code is quoted incorrectly AND you have the WRONG ID.

<button onclick="save(document.getElementById('desTemp').value)">Set</button>

1 Comment

Yeah I fixed that. I'm still getting nothing :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.