You can create an object (avoids global variables except it) then store stuff in it including the functions you need (namespaced) to use!
Here you can try it out: https://jsfiddle.net/MarkSchultheiss/bacy1r18/1/
Sample markup:
Cond:
<input id="conductivity" value="100" /> Blah
<input id="blah" value="bluesblah"> Volt
<input id="voltage" value="100" /> Date
<input id="mydate" type="date" />
<button id="save" type="button">
save
</button>
<button id="show" type="button">
show
</button>
<div id="demoX">
</div>
Code:
var myThings = myThings || {}
myThings.values = {
outputs: [],
labels: {
d: "",
c: "Conductivity",
v: "Voltage",
b: "Blah"
},
defaultDate: new Date()
};
myThings.library = {
formatdate: function(mydate) {
var d = new Date(mydate);
var day = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
return day + '/' + month + '/' + year;
},
saveEm: function() {
var cond = document.getElementById("conductivity").value;
var volt = document.getElementById("voltage").value;
var blah = document.getElementById("blah").value;
// default to now date
var d = document.getElementById("mydate").value ? document.getElementById("mydate").value : myThings.values.defaultDate;
myThings.values.outputs.push({
d: myThings.library.formatdate(d),
cond: cond,
volt: volt,
blah: blah
});
},
showEm: function() {
var arout = "";
for (var i = 0; i < myThings.values.outputs.length; i++) {
arout += "<div class='outrow'>";
arout += "<span class='lab'>" + myThings.values.labels.d + "</span>";
arout += "<span class='outv'>" + myThings.values.outputs[i].d + "</span>";
arout += "<span class='lab'>" + myThings.values.labels.c + "</span>";
arout += "<span class='outv'>" + myThings.values.outputs[i].cond + "</span>";
arout += "<span class='lab'>" + myThings.values.labels.v + "</span>";
arout += "<span class='outv'>" + myThings.values.outputs[i].volt + "</span>";
arout += "<span class='lab'>" + myThings.values.labels.b + "</span>";
arout += "<span class='outv'>" + myThings.values.outputs[i].blah + "</span>";
arout += "</div>";
}
var demo = document.getElementById("demoX");
demo.innerHTML = arout;
}
};
var show = document.getElementById("show");
show.addEventListener("click", myThings.library.showEm);
var save = document.getElementById("save");
save.addEventListener("click", myThings.library.saveEm);
arr.push({date: d, conductivity: conductivity.value, voltage: voltage.value});conductivity;voltage;blahthen on the other side you can convert the string to an array again.