As a newbie, I’m trying to pull something off and have gotten stuck. I’m attempting to select a table row with an id of “fc_cart_foot_tax_tbd” and add a class of “selected” if someone enters a state other than Illinois into an input field with the class of “customer_state_name”. Here’s my HTML that doesn’t work.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Basic</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
//Show the tax info if the shipping state is not Illinois
$("#customer_state_name").each(function()
{
if (!$(this).html() == 'Illinois')
{
$("#fc_cart_foot_tax_tbd").addClass("selected");
}
});
});
</script>
</head>
<body>
<form action="#">
<input name="state" id="customer_state_name" value="" />
</form>
<table>
<tr id="fc_cart_foot_tax_tbd">
<td>Content here!</td>
</tr>
</table>
</body>
</html>
Right a few concerns.
.each()function on an ID. IDs must be unique within a document, use a class if there are multiple items.$(this).html()to get the value of an<input>field, use.val()instead..change()function on the<input>s, so that it runs every time the users alters them.(!a == b)as your condition, this is not what you intend I think, you should use not-equal(a != b).jQuery:
HTML: