0

I currently have the following array in javascript

var chart1data = [
{ "Time": "1", "Temperature": 60, },
{ "Time": "2", "Temperature": 50, },
{ "Time": "3", "Temperature": 42, },
{ "Time": "4", "Temperature": 35, },
{ "Time": "5", "Temperature": 28, },
{ "Time": "6", "Temperature": 24, },
{ "Time": "7", "Temperature": 21, },
{ "Time": "8", "Temperature": 19, },
{ "Time": "9", "Temperature": 18, },
{ "Time": "10", "Temperature": 18, },
];

I have a button, in my HTML which when pressed, should change the value "60" in the above array to another number (for example - 80)

The button links to this function. How can I make it so this works?

 function updatechart (){

//This gets the number from a text box 
 var inputdata1 = document.getElementById("textbox1").innerHTML

//Now I need the code to put this number in replace of the value "60" 
 }
3
  • 1
    Are you using innerHTML to get the value of a text input...? Commented Oct 2, 2013 at 8:37
  • 1
    why in place of 60 and not any other? Commented Oct 2, 2013 at 8:38
  • chart1data[index].Temperature = inputdata1; Commented Oct 2, 2013 at 8:38

3 Answers 3

1

You can update your array as follows:

chart1data[index].Temperature = inputdata1;

Fiddle: http://jsfiddle.net/KyleMuir/sPTG8/1/

Hope this helps

Sign up to request clarification or add additional context in comments.

2 Comments

This works fine. However because the innerHTML is a string and not a value the action I need it for is not work. How do I convert the innerHTML (for exaaple 80 or whatever) to 80 as a value
Try changing innerHTML to value like this: var inputdata1 = document.getElementById("textbox1").value;
0

if you want to change 60 then:

chart1data[0].Temperature = inputdata1;

else

chart1data[index].Temperature = inputdata1;

Comments

0

Simply put, you have an array of object. Said objects contain 2 properties, Time and Temperature. To access an object within the array, assuming you know the index, you can do:

alert(chart1data[index].Time) // alerts the time of the first item
chart1data[index].Temperature = 60; // sets the temperature of the first item

Alternatively, if you want to replace all temperature values that are 60, you can loop through your items and simply update them, like so:

for (var i = 0; i < chart1data.length; i++) {
    if (chart1data[i].Temperature == '60') {
        chart1data[i].Temperature = '80'
    }
}

Comments

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.