How can I add/remove an element to/from the first td of the table before text?
I have a div with id (titlediv_1265601). How can I add/remove a * to the first td of the table before "Adjuster Name:" based on the following condition using jquery?
if(a == 'addSpan')
{}
else if(a == 'RemoveSpan')
{}
<div id="titlediv_1265601" style="width: 100%; position: relative; visibility: visible; display: block;">
<table>
<tbody>
<tr>
<td id="AdjusterOrAdjusterAssistantCell" class="MyTableHeadingCell">A开发者_StackOverflow中文版djuster Name:</td>
<td class="MyTableTagsCell"><input type="text" id="AdjusterOrAdjusterAssistant"></td>
<td class="MyTableHeadingCell"></td>
<td class="MyTableTagsCell"></td>
</tr>
</tbody>
</table>
</div>
$("#AdjusterOrAdjusterAssistantCell")
.prepend('<span class="Required" title="Required">* </span>');
will probably work correctly.
If I understood you correctly: You can use the :first-child
pseudo selector to access the first <td>
in the table, and then jQuery's prepend()
` to prepend your desired content.
$('#titlediv_1265601 table td:first-child').prepend('<span class=“Required” title=“Required”>* </span>');
To add:
$("#AdjusterOrAdjusterAssistantCell").prepend('<span class="Required" title="Required">*</span>');
To remove:
$("#AdjusterOrAdjusterAssistantCell span").remove();
精彩评论