I’m running a really simple jQuery script to grab all emails selected by checkboxes in a table. The table looks like this:
<table>
<tbody>
<tr>
<td>
<input type="checkbox" value="MD5HASH" />
</td>
<td>
First Name
</td>
<td class="email">
Email Address
</td>
</tr>
</tbody>
</table>
My jQuery looks like this:
$("#submitButton").click(function() {
var output = [];
$("table tbody tr:has(input:checkbox:checked)").each(function() {
var email = $('td.email', $(this)).text();
if (validate(email) && output.indexOf(email) == -1)
output.push(email);
});
$("#emails").val(output.join(", "));
});
function validate(email) {
return /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/.test(email);
}
This fails miserably in IE, but works everywhere else.
- The
table tbody tr:has(input:checkbox:checked)selector matches nothing. - The call to validate throws a
Object expectederror.
WHY!? Isn’t jQuery designed to be cross-browser and portable?
Internet Explorer (< 9) doesn’t have
Array.prototype.indexOf. Try using jQuery’s$.inArrayinstead (it’s cross browser and will actually useArray.prototype.indexOfif it exists :-P).