I am trying to cut some code from my esp8266
script by using a function to change the href
address in the browser when a button is clicked. I want the id number to be stored in the variable so that I can reuse it to set the pin number if that particular button id is clicked.
function myFunction() {
console.log("button was clicked");
var x = document.getElementById(Element.id);
console.log(x);
document.getElementById(x).setAttribute("onClick",
"javascript:window.location.href='/toggle?pin_number='" + x);
}
<button class="button" id="4" onClick="myFunction()">TOGGLE D4</button><br>
<button class="button" id="5" onClick="javascript:window.location.href='/toggle?pin_number=5'">TOGGLE D5</button><br>
<button class="button" id="6" onClick="javascript:window.location.href='/toggle?pin_number=6'">TOGGLE D6</button><br>
the console.log(x)
returns null
.
I don't know whether this is the best way to do it or not because I'm new to javascript
.