0

This is my view page:

for (int i = 0; i < Model.Count; i++)
{
  <div class="row service-block" id="searchresult@(i)">
    <div class="col-lg-6 col-md-5">
      <div class="mrb5">
        <label>abc</label>
        <label>Xyz</label>
        <input type="button" style="display:none;"class="bookappointmentbutton" id="btnAppointment" value="Book Appointment" />
      </div>
    </div>
  </div>
}

And the JavaScript:

$(document).ready(function () {
  $("#searchresult").mouseenter(function () {
    $('#btnAppointment').show();
  });
  $("#searchresult").mouseleave(function () {
    $('#btnAppointment').hide();
  });
});

I am displaying list of records like this:

enter image description here

consider this image as an example because this is how i am displaying list of records.

now i want to do is when my mouse hovers over div say for eg searchresult0 then i want to show button and on mouseleave i want to hide button.

when my mouse hover over searchresult1 then i want to display button and on leave hide button.

but as i am generating new div for every records i am not getting how to to this with different division.

2
  • 1
    FYI: IDs must be unique on document context Commented Oct 29, 2014 at 11:15
  • What is the outer element, in which these "searchresult" elements sit? Commented Oct 29, 2014 at 11:25

1 Answer 1

1

Use this to find element in currently hovered div. also you have duplicate IDs in DOM. IDs should be unique. use class selector instead of keeping duplicate IDs:

    $("[id^=searchresult]").mouseenter(function () {
         $(this).find('.bookappointmentbutton').show();
    });
    $("[id^=searchresult]").mouseleave(function () {
        $(this).find('.bookappointmentbutton').hide();
    });

As suggested by A. Wolff, You can narrow down these two event to single event by using .hover()

$("[id^=searchresult]").hover(function () { $(this).find('.bookappointmentbutton').toggle(); }); 

Working Demo

Sign up to request clarification or add additional context in comments.

3 Comments

+1 but could be simplified as: $("[id^=searchresult]").hover(function () { $(this).find('.bookappointmentbutton').toggle(); });
Indeed. but not sure whether delegation is required or not here as OP is looping and creating element dynamically. and hover wont be delegated.
But here you aren't delegating events anyway ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.