0

I want to to translate each of these elements with the same class .. for example if I hover one of these classes I want just the class hovered to be translated not all of them .. Here is my code :

<div class="latestNewsHeadLines">

 <ul>

   <li class="headlineCl">text1</li>
   <li class="headlineCl">text2</li>
   <li class="headlineCl">text3</li>       
   <li class="headlineCl">text4</li>    

 </ul>

</div>

jQuery Code :

 $(".headlineCl").hover(function(){

    var Length = $(this).width();
    var transRight = -(Length -245) ;


    $(".headlineCl a").css({'right':transRight+"px"})


});

Thank you!

3
  • 1
    use this context Commented Aug 23, 2016 at 3:52
  • There is no <a> tag under class="headlineCl" in HTML Commented Aug 23, 2016 at 3:57
  • hahah yes .. i made a mistake whrn typing but it should to be there Commented Aug 23, 2016 at 4:06

2 Answers 2

2

If your html actually has an a tag unlike the provided html:

Codepen

 $(".headlineCl").hover(function(){
    var Length = $(this).width();
    var transRight = -(Length -245) ;
    // `this` refers to the selector to which 
    // your hover function is attached 
    $(this).find('a').css({'right':transRight+"px"})
});

Otherwise, you can use:

$(".headlineCl").hover(function(){
    var Length = $(this).width();
    var transRight = -(Length -245) ;
    // `this` refers to the selector to which 
    // your hover function is attached
    $(this).css({'right':transRight+"px"})
});
Sign up to request clarification or add additional context in comments.

Comments

0

$(".headlineCl").hover(function(){

    var Length = $(this).width();
    var transRight = -(Length -245) ;
  
    $("a").css('color','black')//put color black on all anchor element(remove red color)
    $("a",this).css('color','red')// use this context to tell the hovered element. this will put color red to hovered element


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="latestNewsHeadLines">

 <ul>

   <li class="headlineCl"><a>text1</a></li>
   <li class="headlineCl"><a>text2</a></li>
   <li class="headlineCl"><a>text3</a></li>       
   <li class="headlineCl"><a>text4</a></li>    

 </ul>

</div>

  1. Use this context to tell the element being hovered

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.