Is there a way to change html element on hovering on it in ASP.Net page

Viewed 40

The tempdata populates a div in the aspx page. Whenever the parent div - SomeClassParent is being hovered on, the child span text - SomeClassChild3 string should be shown. How do I achieve that with jQuery?

Below is the js portion of the code

function ShowDiv(Somedata){
 for(var v in Somedata){
    var tempdata = ''
    tempdata += '<div class="SomeClassParent">'
    tempdata += '<div class="SomeClassChild">'+Somedata[v].serialnum+'</div>'
    tempdata += '<span class="SomeClassChild2">'+Somedata[v].textdata.slice(0,49).trim()+'</span>'
    tempdata += '<span class="SomeClassChild3" style="display:none;">'+Somedata[v].textdata.slice(49,v.textdata.length).trim()+'</span>'
    tempdata += '</div>'
}
}


$(document).ready(function () {
    $('.SomeClassParent').hover(function () {
        $(".SomeClassChild3").css({"display":"block"});
    });
    });
1 Answers

I assume you want these to collapse when not hovering over them. So, you'll need to use mouseenter and mouseleave versus just hover.

You'll also want to find the parent's children, not the blanket class "SomeClassChild3" or all elements with that class will display when you hover over anything with the class "SomeClassParent"

$(document).ready(function () {
    $('.SomeClassParent').mouseenter(function () {
        $(this).find(".SomeClassChild3").css({"display":"block"});
    });
    $('.SomeClassParent').mouseleave(function () {
        $(this).find(".SomeClassChild3").css({"display":"none"});
    });
});
.SomeClassParent{
  width: 30%;
  float: left;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="SomeClassParent">
  <div class="SomeClassChild">Child</div>
  <span class="SomeClassChild2">Child2</span>
  <span class="SomeClassChild3" style="display:none;">Child3</span>
</div>

<div class="SomeClassParent">
  <div class="SomeClassChild">Child</div>
  <span class="SomeClassChild2">Child2</span>
  <span class="SomeClassChild3" style="display:none;">Child3</span>
</div>

<div class="SomeClassParent">
  <div class="SomeClassChild">Child</div>
  <span class="SomeClassChild2">Child2</span>
  <span class="SomeClassChild3" style="display:none;">Child3</span>
</div>

Related