PDA

View Full Version : How do I repeat this code



padders01
05-16-2014, 03:00 PM
Hi,

I have a jsfiddle which I have seen off another site.

http://jsfiddle.net/XssCG/307/

It does the job perfectly, but I want to use the same code four or five times. So in one form use this Yes/No drop down list and depending on the answer the other drop lists become available.

Just so it's clear I want the exact same as I have but a repeat of it.

How do I do this?

I've tried so many different ways, I just can't work it out

Thank you

jscheuer1
05-16-2014, 03:12 PM
As long as you keep to the format of a unique id with no number for the yes/no select and that same id with the number 1 added at the end for the select that it is to control, this will do it:


function check(elem) {
document.getElementById(elem.id + '1').disabled = !elem.selectedIndex;
}

padders01
05-16-2014, 03:25 PM
Thanks for your reply, I couldn't get it to work though

http://jsfiddle.net/XssCG/311/

jscheuer1
05-16-2014, 03:38 PM
Id's must be unique and follow the pattern this works:


<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function check(elem) {
document.getElementById(elem.id + '1').disabled = !elem.selectedIndex;
}
</script>
</head>
<body>
<form>
<select id="mySelect" onChange="check(this);">
<option>Yes</option>
<option>No</option>
</select>

<select id="mySelect1" disabled="disabled" >
<option value=" "> </option>
<option value="Claim on hold">Claim on hold</option>
<option value="No reference available">No reference available</option>
<option value="Customer cancelled claim">Customer cancelled claim</option>
</select>
<select id="mySelect2" onChange="check(this);">
<option>Yes</option>
<option>No</option>
</select>

<select id="mySelect21" disabled="disabled" >
<option value=" "> </option>
<option value="Claim on hold">Claim on hold</option>
<option value="No reference available">No reference available</option>
<option value="Customer cancelled claim">Customer cancelled claim</option>
</select>
</form>

</body>
</html>

padders01
05-16-2014, 03:44 PM
That brilliant, thanks for your help