Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving trouble traversing DOM to get div occuring just before another element
    primarykey
    data
    text
    <p>The value of previousDiv when i run the jQuery shown below is always: [ ]. I have tried several variations of the .prev() function including .prev('div'), and .prev() all with the same result.</p> <p>Here is the jQuery:</p> <pre><code>$('.AddToRole').click(function(evt) { var path = document.URL; var thisUser = this.value; var previousControl = $(this).prev('.AvailableRoles'); var newUserRole = previousControl[0].options[previousControl[0].selectedIndex].text; var newHTML = "&lt;button id='button_DeleteRole_" + newUserRole +"' class='DeleteRole' value=" + newUserRole + "&gt;X&lt;/button&gt;&amp;nbsp;&lt;label id='label_DeleteRole_" + newUserRole + "'&gt;" + newUserRole + "&lt;/label&gt;&lt;br /&gt;"; var previousDiv = $(this).prev('#DisplayUserRoles'); $(previousDiv).append(newHTML); </code></pre> <p>Here is the html/razor code in the page:</p> <pre><code>&lt;div&gt; @* Display user email. *@ &lt;h3&gt;@user.Email&lt;/h3&gt; &lt;div&gt; &lt;div id="DisplayUserRoles"&gt; @{ @* Display roles user belongs to. *@ foreach (var role in userRoles) { @* Remove user from role button. *@ &lt;button id="button_RemoveFromRole_@user.Email" value="@role.RoleName" onclick="RemoveUserFromRole(this);"&gt;X&lt;/button&gt; &lt;label id="label_RemoveFromRole_@role.RoleName"&gt;@role.RoleName&lt;/label&gt; &lt;br /&gt; } } &lt;/div&gt; &lt;br /&gt; @{ @* Create ListBox with roles user does not belong to. *@ var items = rolesUserDoesNotBelongTo.Select(i =&gt; new SelectListItem { Value = i.RoleName, Text = i.RoleName }); string listBoxId = "listboxRoles_"; listBoxId = listBoxId + user.Email; } @Html.ListBox(listBoxId, items, new { @class = "AvailableRoles" }) @* Add user to role button. *@ &lt;button id="button_AddToRole_@user.Email" class="AddToRole" value="@user.Email"&gt;Add To Role&lt;/button&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>For the end result I am trying to add another button and label just before the closing div tag for #DisplayUserRoles.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload