Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8696575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:18:20+00:00 2026-06-13T01:18:20+00:00

The value of previousDiv when i run the jQuery shown below is always: [

  • 0

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.

Here is the jQuery:

$('.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 = "<button id='button_DeleteRole_" + newUserRole +"' class='DeleteRole' value=" + newUserRole + ">X</button>&nbsp;<label id='label_DeleteRole_" + newUserRole + "'>" + newUserRole + "</label><br />";
var previousDiv = $(this).prev('#DisplayUserRoles');
$(previousDiv).append(newHTML);

Here is the html/razor code in the page:

<div>
                    @* Display user email. *@
                    <h3>@user.Email</h3>
                    <div>
                        <div id="DisplayUserRoles">
                            @{
                                @* Display roles user belongs to. *@
                                foreach (var role in userRoles)
                                {
                                    @* Remove user from role button. *@
                                    <button id="button_RemoveFromRole_@user.Email" value="@role.RoleName" onclick="RemoveUserFromRole(this);">X</button>
                                    <label id="label_RemoveFromRole_@role.RoleName">@role.RoleName</label>
                                    <br />
                                }
                            }
                        </div>
                        <br />                            
                    @{                                                    
                        @* Create ListBox with roles user does not belong to. *@
                        var items = rolesUserDoesNotBelongTo.Select(i => 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. *@
                        <button id="button_AddToRole_@user.Email" class="AddToRole" value="@user.Email">Add To Role</button>
                    </div>
                </div>

For the end result I am trying to add another button and label just before the closing div tag for #DisplayUserRoles.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:18:21+00:00Added an answer on June 13, 2026 at 1:18 am

    .prev() does not work how you are using it. It gets the previous sibling element and then filters that by the selector you pass. It does NOT look for any previous sibling that matches the selector.

    Thus, when you do both of these:

    $(this).prev('.AvailableRoles');
    $(this).prev('#DisplayUserRoles');
    

    At least one of them will return no object unless the previous sibling is both .AvailableRoles and #DisplayUserRoles which I doubt is what you’re looking for.


    I’m not exactly sure I know what you’re trying to do with these, but you could do this instead:

    $(this).prevAll('.AvailableRoles').first();
    

    And for the DisplayUserRoles, since there can only ever be one object with a given ID in the document, you can just do this:

    $('#DisplayUserRoles');
    

    These would get all previous siblings that match the selector and then get just the first one.


    As I said in my comment, we could help a lot better if you posted the generated HTML (what a browser sees with View/Source) rather than you template code which we don’t necessarily know what HTML it generates.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

var value; $(#Grid1).click(function(e) { var row = jQuery(e.target).parent(); value= row.attr(id); }); var onrowclick =
The value for $i in the code below is always 2. It seems it
given value = 2 or 4.00 the below statement outputs value = Convert.ToDecimal(value).ToString(C2); value
I'm using <script> $(#a-slots-16-history div:nth-child(odd)).addClass(function() { return slick-toggle + $(this).index(); }); </script> to give
Value objects do not have identity. ORM needs identity to update the database. How
First Value: I have a binary value which is actually a compact series of
I use jquery and I want to find the previous div which has the
need value advice and help me to sort out the following issue. function max_key($array)
SELECT value FROM table WHERE id IN (1,2,3, ... 500000) Have a query that
My value: ingrid lovesc cops and taxi I have a mysql table with a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.