I am (attempting) to have a table which can switch the background color based on an RGB value, where each row of the table has clickable <td>
s, and they will add up to give and RGB value (eg. first row is +/- 123, second row is +/- 123.
#2: The issue I'm running into currently is that when I attempt to pass my DOM object from my listener-creating for loop to the switch_JS' function, the DOM that is being passed in
domobj` is undefined.
I would then like to switch the value of the hidden inputs inside of the <td>
s each time the user clicks, and define a separate function (probably not in the JS) to add them up and render a rgb color based on that.
As I said, the real issue is above as #2, but any other help would be greatly appreciated. I am very new to programming in general, and this is mainly for my own learning.
<script>
var JS_elements = document.getElementsByClassName("JS")
for (y = 0; y < JS_elements.length; y++){
x = JS_elements[y].getElementsByTagName("input")[0]
alert(String(x.value)) **this loop runs 3 times, and puts 'false' to the alert pop-up each time
JS_elements[y].addEventListener('click', function() {
switch_JS(JS_elements[y].getElementsByTagName("input")[0]);
});
function testfunc() {
alert("TestFunc");
}
function true_switch(val) {
document.getElementById("testblock").innerHTML += "true_switch worked; "; <!-- debug line -->
if (val == true) {
return false;
} else if (val == false) {
return true;
} else {
alert("Error #475989 in function true_switch");
}
}
function switch_JS(domobj) {
<!-- takes as input an HTML object, and switched the value from true to false, or from false to true -->
document.getElementById("testblock").innerHTML = document.getElementById("testblock").innerHTML + "Step 1 worked; "; <!-- debug line -->
alert(String(domobj)); <!-- debug line -->
val = domobj.querySelector("input").value;
document.getElementById("testblock").innerHTML += "Step 2 worked; "; <!-- debug line -->
if ((typeof val) != Boolean) {
alert("Error, non-boolean passed into switch_JS");
document.getElementById("testblock").innerHTML += "1st if worked; ";
} else {
domobj.querySelector("input").value = true_switch(HTML.value);
document.getElementById("testblock").innerHTML += "else worked; "; <!-- debug line -->
}
}
</script>
HTML:
<body>
<div id="testblock">Testblock: </div>
<header>
<hr>
<p>- Header Background Color Controller -</p>
<table>
<tr>
<td>Javascript Controller:</td>
<td class="JS">Red
<input type="hidden" value='false'>
</td>
<td class="JS">Green
<input type="hidden" value='false'>
</td>
<td class="JS">Blue
<input type="hidden" value='false'>
</td>
</tr>
<tr>
<td>jQuery Controller:</td>
<td class="jQ" value=false>Red</td>
<td class="jQ" value=false>Green</td>
<td class="jQ" value=false>Blue</td>
<tr>
</table>
<hr>
</header>
<div class="main_div">
</div>
this
instead of the array elementval = domobj.querySelector("input").value;
FIDDLE
and then this will work when you pass in (this)
Update for 2020
but I suggest you delegate to the nearest static container