Reputation: 53
I have a bunch of buttons with the same name, but different values. These buttons need to change from one value to another and back when clicked. I know I can do it using html and javascript, but I want to do it with as little code as possible. I have 13 buttons and I don't want to call 13 different onclick
functions. I was hoping to use a case statement with the element ids, but I don't know I where to begin. I would post my code, but it's so simple I doubt it's needed.
Here's an example of what my code for the buttons looks like:
<tr>
<td>
<input type="button" value="test1" name="Online">
</td>
<td>
<input type="button" value="test2" name="Online">
</td>
</tr>
Upvotes: 0
Views: 2117
Reputation: 1662
If you don't need to do anything server-side, you can do this using just javascript. I've posted an example of how to do this here.
First, just add an onclick
event that passes the current element along to a function, say clicked()
.
<tr>
<td>
<input onclick="clicked(this)" type="button" value="test1" name="Online">
</td>
<td>
<input onclick="clicked(this)" type="button" value="test2" name="Online">
</td>
</tr>
Here's what the javascript clicked()
function would look like
function clicked(elem) {
if (elem.name === 'Online') {
elem.name = 'Offline';
// do other stuff
} else {
elem.name = 'Online';
// do other stuff
}
}
Upvotes: 1
Reputation: 16321
Since your buttons have the same name, you won't be able to use the default OnClick event (in this case Online_OnClick
). If you try, only the last button will fire the event. But you can use the onclick
attribute to specify a function to call. For example:
<input type="button" value="test1" name="Online" onclick="DoButtonClick('test1')">
<input type="button" value="test2" name="Online" onclick="DoButtonClick('test2')">
<script language="vbscript">
Sub DoButtonClick(strButtonValue)
' Do what you wish
End Sub
</script>
Upvotes: 1