|
Posted by GregAgee on 09/01/06 21:59
Hey there Afrinspray,
To make sure I understand your question, you only want to perform some
action when a row is clicked, but not specific elements in that row?
One way to solve this problem is, first, to figure out what element the
user clicked (i.e. a selection list, or a checkbox, etc). Then create
an array of those elements' ids to check against. With this array you
can traverse though it and check to see if the element that was clicked
is in the 'noAction' array, otherwise, perform the desired action.
Here is some example code that you can check out (works on firefox and
IE 6):
<html>
<head>
<title>MouseClick Test</title>
</head>
<body>
<script language="javascript" type="text/javascript">
/* do something unless certain elements were clicked... */
function doSomething(mouseEvent) {
var elementID;
//if event doesn't exist,
//use window.event
if(!mouseEvent)
mouseEvent = window.event;
// get the id of the element that was clicked (firefox)
if(mouseEvent.target)
elementID = mouseEvent.target.id;
// get the id of the element that was clicked (IE)
else if(mouseEvent.srcElement)
elementID = mouseEvent.srcElement.id;
// perform the desired action UNLESS these
// elements are clicked
var noActionIDs = new Array();
noActionIDs[0] = "checkboxID";
noActionIDs[1] = "selectID";
for(var i = 0; i < noActionIDs.length; i++) {
if(elementID == noActionIDs[i])
return;
}
performAction();
}
/* alert when the row was clicked, but not the
* specified elements
*/
function performAction() {
alert("The row was clicked!");
}
</script>
<table border="1">
<tr onclick="doSomething(event)">
<td>
<input type="checkbox" id="checkboxID" />
</td>
<td>
<select id="selectID">
<option>Some Option</option>
</select>
</td>
<td>
<span>blah blah blah blah</span>
</td>
<td>
<span>bloo bloo bloo bloo</span>
</td>
</tr>
</table>
</body>
</html>
(hope this helps)
afrinspray@gmail.com wrote:
> I have a table row with a dropdown and a checkbox, but I want the table
> rows onClick event to fire only when the user clicks the background of
> the row and not the dropdown or checkbox. How can I do this?
>
> Thanks,
> Mike
[Back to original message]
|