How could I write this in javascript or jQuery:
When I push the button the first time the onoff var in the change function gets the value off and the off button changes to a button named on;
now when I press the on button the var onoff gets the value on and the button changes to an off button again.
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="jquery-min.js"></script>
<script type="text/javascript">
function change( inputId ) {
/* ... do something with inputId ... */
var onoff = 'off';
console.log( onoff );
}
</script>
</head>
<body>
<form>
<table>
<tr>
<td>one:</td><td><input name="one" id="one" /></td>
<td><input type="button" id="b_one" value="off" onclick="change('one')"></td>
</tr>
<tr>
<td>two:</td><td><input name="two" id="two" /></td>
<td><input type="button" id="b_two" value="off" onclick="change('two')"></td>
</tr>
</table>
<br /><br /><input type="submit" value="ok"/></div><br />
</form>
</body>
</html>
This is in “pure” JavaScript. You will retrieve the button elements for given input and change its value based on the previous value.
HERE is the code.
Your
change()function won’t work because ‘off’ is assigned toonoffvariable everytime the function is called. You have to move the variable definition outside thechange()function if you want to use it.