0

I want increase the font size of parent element. When i use my code it will increase full body font size how can i avoid that issue? Example based on the id="vs-1" i want increase the font size of the grant parent li text here 1940 .

$("li #vs-1").parents().css({
  'color': '#ff3600',
  'font-size': '35px'
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>1940
    <ul>
      <li style="background-color: rgb(255, 54, 0);" id="vs-1"></li>
    </ul>
  </li>
  <li>1970
    <ul>
      <li id="vs-2"></li>
    </ul>
  </li>
</ul>

When i use this code it will affect entire body ul li tag . Please give me the solution. Thanks

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
Rijo
  • 2,963
  • 5
  • 35
  • 62

4 Answers4

2

Updated: need to reach parent and then use closest(). Please check below snnippet

$(function(){
  $("li #vs-1").parent().closest("li").css({'color':'#ff3600','font-size':'35px'});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>1940
    <ul>
      <li style="background-color: rgb(255, 54, 0);" id="vs-1"></li>
    </ul>
  </li>
  <li>1970
    <ul>
      <li id="vs-2"></li>
    </ul>
  </li>
</ul>
Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
  • do you get any error message in your console when trying this answer – Yannick Huber Feb 03 '17 at 15:02
  • **4 upvotes fot a non-working answer !!** Sorry but the `closest()` will not work in this situation.. Take a look to **[closest() method not working as expected](http://stackoverflow.com/questions/41508008/closest-method-not-working-as-expected)**. – Zakaria Acharki Feb 03 '17 at 15:10
  • @Rijo, yes it was not working .. I have corrected my answer , please check – Bhushan Kawadkar Feb 03 '17 at 15:16
2

You were on the right path you should just to specify the parent in .parents() call :

$('li #vs-1').parents("li").css({'color':'#ff3600','font-size':'35px'});
______________________^^^^

Hope this helps.

$("li #vs-1").parents('li').css({
  'color': '#ff3600',
  'font-size': '35px'
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>1940
    <ul>
      <li style="background-color: rgb(255, 54, 0);" id="vs-1"></li>
    </ul>
  </li>
  <li>1970
    <ul>
      <li id="vs-2"></li>
    </ul>
  </li>
</ul>
hungerstar
  • 21,206
  • 6
  • 50
  • 59
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
1

A few things:
To achive what you want, you can use $('...').parents()[1].css({'...'});
the .parents() method returns an array of all parents, and the first element of the array is the immediate parent.
Also, since you have an id of the element you want to select, you don't have to have li before it.
See the docs

1

You only want style parent li, not that li itself. Try this:

$("li #vs-1").parent('li').css({
  'color': '#ff3600',
  'font-size': '35px'
});

$("li #vs-1").closest('li').css({
  'color': '#000',
  'font-size': '16px'
});

https://jsfiddle.net/9u9hb839/2/

mudin
  • 2,672
  • 2
  • 17
  • 45
  • ya its working thanks for your work. So my question is valid can u give me vote for my question – Rijo Feb 03 '17 at 15:18