I have a task from a client to correct a page and I found that jQuery descendant selector does not behave as expected.
Here is an excrept of the HTML:
<form action="http://submit.url/subscribe.php" method="post" enctype="multipart/form-data" id="mssysform8217" class="mssysform" >
<div id="mssys-formcontainer">
<div class="formfields">
<table style="width: 100%">
<div class="formfield-item" id="formfield-item-email">
<tr>
<td class="style1"><label >E-mail címe</label></td>
<td>
<input type="text" name="email" value="">
<div class="error-container">Please fill in this field!</div>
<div style="clear: both;"></div>
</td>
</tr>
</div>
</table>
</div>
</div>
</form>
I tried to debug it with FireFox:
this worked:
console.debug($("#mssysform8217 :input[name='email']").val());
this did not worked:
console.debug($("#mssysform8217 #formfield-item-email :input[name='email']").val());
undefined
but:
console.debug($("#mssysform8217 #formfield-item-email"));
[div#formfield-item-email.formfield-item]
The problem is that the submit script is generated by a third party app and it wants to use the 3 level descendant selector.
jQuery selectors allow you to select and manipulate HTML element(s). jQuery selectors are used to "find" (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more. It's based on the existing CSS Selectors, and in addition, it has some own custom selectors.
jQuery :not() Selector The :not() selector selects all elements except the specified element. This is mostly used together with another selector to select everything except the specified element in a group (like in the example above).
In jQuery, the $ sign is just an alias to jQuery() , then an alias for a function. This page reports: Basic syntax is: $(selector).action() A dollar sign to define jQuery.
The jQuery selector you are trying to use will not work because the HTML is invalid.
<table style="width: 100%">
<div class="formfield-item" id="formfield-item-email">
<tr>
This is not valid HTML. It is not valid to place a div (or any element other than <thead>
, <tfoot>
, <tbody>
, <tr>
) in the middle of a table outside of a cell.
This is valid:
<div>
<table>
<tr><td></td></tr>
</table>
</div>
Or this is valid:
<table>
<tr><td><div></div></td></tr>
</table>
On a side note: You are using the table to format your form. Tables are meant for tabular data. Using tables for layout is a bad idea in my book. Use proper semantics for HTML elements (tables = tabular data, li=lists, div=page divisions, etc...).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With