Ok, I'm quite new to javascript, and so far have managed to get through what I was attempting to do, but have come to a small problem I just can't get past after hours of searching.
I'm currently working on a script for a small online application, and ontop of the functions I've done already I want them to:
Upon adding a value to a hidden field, hide the other radio buttons with the same value and then to show the radio buttons again if the value in that field changes.
I've started, but it's just not working, and any advice would be very much appreciated.
Here's the html code:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta http-equiv="Content-Language" content="en-gb" />
<title>Test</title>
<script type="text/javascript" src="custom.js"></script>
</head>
<body>
<div id="content">
<div id="showcrit1">Lagu Wind Seeker<div id="showdefcrit1"><strong>Not Defended</strong></div></div>
<a href="#" onclick="showhide('selectcrit1'); return(false);">Select Crit 1</a>
<div id="selectcrit1" style="width: 400px; background: #666666; border: 1px solid black; padding: 10px; display: none">
<strong>Select Crits</strong>
<br />
<br />
<form name="selectcrit1">
<input type="radio" id="Not Defended" name="crit1" value="0" onclick="addcrit(1);" checked="checked" /> Not Defended<br />
<input type="radio" id="dfgdfg" name="crit1" value="1" onclick="addcrit('1');" /> dfgdfg<br />
<input type="radio" id="Poisonspinner Shaitan" name="crit1" value="6" onclick="addcrit('1');" /> Poisonspinner Shaitan<br />
<input type="radio" id="Phoenix Alerion" name="crit1" value="7" onclick="addcrit('1');" /> Phoenix Alerion<br />
<input type="radio" id="Ayor Wind Keeper" name="crit1" value="9" onclick="addcrit('1');" /> Ayor Wind Keeper<br />
<input type="radio" id="Ayor Wind Keeper" name="crit1" value="11" onclick="addcrit('1');" /> Ayor Wind Keeper<br />
<input type="radio" id="Putrid Si'lah" name="crit1" value="14" onclick="addcrit('1');" /> Putrid Si'lah<br />
</form>
</div>
<br />
<br />
<div id="showcrit2">Lagu Wind Seeker<div id="showdefcrit2"><strong>Not Defended</strong></div></div>
<a href="#" onclick="showhide('selectcrit2'); return(false);">Select Crit 2</a>
<div id="selectcrit2" style="width: 400px; background: #666666; border: 1px solid black; padding: 10px; display: none">
<strong>Select Crits</strong>
<br />
<br />
<form name="selectcrit2">
<input type="radio" id="Not Defended" name="crit2" value="0" onclick="addcrit(2);" checked="checked" /> Not Defended<br />
<input type="radio" id="dfgdfg" name="crit2" value="1" onclick="addcrit('2');" /> dfgdfg<br />
<input type="radio" id="Poisonspinner Shaitan" name="crit2" value="6" onclick="addcrit('2');" /> Poisonspinner Shaitan<br />
<input type="radio" id="Phoenix Alerion" name="crit2" value="7" onclick="addcrit('2');" /> Phoenix Alerion<br />
<input type="radio" id="Ayor Wind Keeper" name="crit2" value="9" onclick="addcrit('2');" /> Ayor Wind Keeper<br />
<input type="radio" id="Ayor Wind Keeper" name="crit2" value="11" onclick="addcrit('2');" /> Ayor Wind Keeper<br />
<input type="radio" id="Putrid Si'lah" name="crit2" value="14" onclick="addcrit('2');" /> Putrid Si'lah<br />
</form>
</div>
<br />
<br />
<div id="showcrit3">Lagu Wind Seeker<div id="showdefcrit3"><strong>Not Defended</strong></div></div>
<a href="#" onclick="showhide('selectcrit3'); return(false);">Select Crit 3</a>
<div id="selectcrit3" style="width: 400px; background: #666666; border: 1px solid black; padding: 10px; display: none">
<strong>Select Crits</strong>
<br />
<br />
<form name="selectcrit3">
<input type="radio" id="Not Defended" name="crit3" value="0" onclick="addcrit(3);" checked="checked" /> Not Defended<br />
<input type="radio" id="dfgdfg" name="crit3" value="1" onclick="addcrit('3');" /> dfgdfg<br />
<input type="radio" id="Poisonspinner Shaitan" name="crit3" value="6" onclick="addcrit('3');" /> Poisonspinner Shaitan<br />
<input type="radio" id="Phoenix Alerion" name="crit3" value="7" onclick="addcrit('3');" /> Phoenix Alerion<br />
<input type="radio" id="Ayor Wind Keeper" name="crit3" value="9" onclick="addcrit('3');" /> Ayor Wind Keeper<br />
<input type="radio" id="Ayor Wind Keeper" name="crit3" value="11" onclick="addcrit('3');" /> Ayor Wind Keeper<br />
<input type="radio" id="Putrid Si'lah" name="crit3" value="14" onclick="addcrit('3');" /> Putrid Si'lah<br />
</form>
</div>
<br />
<br />
<div id="showcrit4">Lagu Wind Seeker<div id="showdefcrit4"><strong>Not Defended</strong></div></div>
<a href="#" onclick="showhide('selectcrit4'); return(false);">Select Crit 4</a>
<div id="selectcrit4" style="width: 400px; background: #666666; border: 1px solid black; padding: 10px; display: none">
<strong>Select Crits</strong>
<br />
<br />
<form name="selectcrit4">
<input type="radio" id="Not Defended" name="crit4" value="0" onclick="addcrit(4);" checked="checked" /> Not Defended<br />
<input type="radio" id="dfgdfg" name="crit4" value="1" onclick="addcrit('4');" /> dfgdfg<br />
<input type="radio" id="Poisonspinner Shaitan" name="crit4" value="6" onclick="addcrit('4');" /> Poisonspinner Shaitan<br />
<input type="radio" id="Phoenix Alerion" name="crit4" value="7" onclick="addcrit('4');" /> Phoenix Alerion<br />
<input type="radio" id="Ayor Wind Keeper" name="crit4" value="9" onclick="addcrit('4');" /> Ayor Wind Keeper<br />
<input type="radio" id="Ayor Wind Keeper" name="crit4" value="11" onclick="addcrit('4');" /> Ayor Wind Keeper<br />
<input type="radio" id="Putrid Si'lah" name="crit4" value="14" onclick="addcrit('4');" /> Putrid Si'lah<br />
</form>
</div>
<br />
<br />
<br />
<form name="critlist" action="defend.php?action=defend&id=2" method="post">
<input type="hidden" name="crit1" value="" id="crit1" />
<input type="hidden" name="crit2" value="" id="crit2" />
<input type="hidden" name="crit3" value="" id="crit3" />
<input type="hidden" name="crit4" value="" id="crit4" />
<input type="submit" value="Defend Attack" />
</form>
</div>
</body>
</html>
and the javascript
:
Code:
function showhide(id) {
if (document.getElementById) {
obj = document.getElementById(id);
if (obj.style.display == "none") {
obj.style.display = "block";
} else {
obj.style.display = "none";
}
}
}
function addcrit(number) {
var total = 0;
for (i = 0; i < 7; i++) {
if (document.forms['selectcrit' + number].elements[i].checked) {
total = document.forms['selectcrit' + number].elements[i].value;
showcrit(document.forms['selectcrit' + number].elements[i].id, 'showdefcrit' + number);
}
}
var newh = document.getElementByValue(total);
var count = newh.length-1;
for ( i = count; i >= 0; i-- ) {
newh[i].parentNode.removeChild(newh[i]);
}
document.getElementById('crit' + number).value = total;
showhide('selectcrit' + number);
}
function showcrit(crit, div) {
document.getElementById(div).innerHTML = 'Vs. ' + crit;
}
the script was working perfectly to do what I wanted like I said, then upon deciding to attempt to remove items aswel once selected, it stopped working, I imagine I completly got it wrong though, the part I added extra was:
Code:
var newh = document.getElementByValue(total);
var count = newh.length-1;
for ( i = count; i >= 0; i-- ) {
newh[i].parentNode.removeChild(newh[i]);
}
Any advice would be great thanks.