9780

How to add a class to previous div [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/11134794/prev-and-next-usage-clarification-jquery" dir="ltr" rel="nofollow">prev(); and next(); usage clarification Jquery</a> <span class="question-originals-answer-count"> 4 answers </span> </li> </ul>

I need to add class to the first div before the "current" class.

<div></div> <div></div> <div class="current">Current</div> <div ></div> <div></div>

I just found out how to do it for the next div but still looking for solution to get the class before it.

$(document).ready(function(){ $(".current + div").addClass("prv"); });

<a href="https://fiddle.jshell.net/0d7csqgb/2/" rel="nofollow">https://fiddle.jshell.net/0d7csqgb/2/</a>

Do anyone know how I can do it?

//EDIT

Here's working solution if somebody need: <a href="https://fiddle.jshell.net/df9ef0hf/6/" rel="nofollow">https://fiddle.jshell.net/df9ef0hf/6/</a>

Answer1:

You can use prev function..

<pre class="snippet-code-js lang-js prettyprint-override">$(document).ready(function() { $(".current").prev().addClass("prv"); }); <pre class="snippet-code-css lang-css prettyprint-override">.prv{ background-color: red; } <pre class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>one</div> <div>two</div> <div class="current">Current</div> <div>tree</div> <div>four</div>

Answer2:

<pre class="snippet-code-js lang-js prettyprint-override">$(document).ready(function(){ $(".current").prev().addClass("prv"); $(".current").next().addClass("next"); }); <pre class="snippet-code-css lang-css prettyprint-override">.prv{color:red;} .next{color:green;} <pre class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>...</div> <div>prv</div> <div class="current">Current</div> <div >next</div> <div>...</div>

Answer3:

Try the following:

<pre class="snippet-code-js lang-js prettyprint-override">$(document).ready(function(){ $('.current').prev('div').addClass('prv'); }); <pre class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div></div> <div></div> <div class="current">Current</div> <div ></div> <div></div>

Recommend

  • t:dataScroller not working correctly on refresh
  • Error incomplete definition of type 'struct ec_key_st' using OpenSSL
  • Get Prevision Next record in ONE ROW
  • Algorithm to create flow chart [A little guidance??]
  • Increasing dimensions on hover without changing the position of other elements
  • How to keep button state across different pages?
  • Get css animation property with jQuery
  • What is the use of a session store?
  • Uncaught TypeError: $(…).select2 is not a function
  • Button click event not firing in jQuery
  • How do I get HTML corresponding to current DOM tree?
  • chrome.tabs.executeScript only fires when the Developer Console is open
  • JQuery Internet Explorer and ajaxstop
  • JSON response opens as a file, but I can't access it with JavaScript
  • RectangularRangeIndicator format like triangular using dojo
  • Excel - Autoshape get it's name from cell (value)
  • Menu Color Fade on Hover with Jquery
  • Cross-Platform Protobuf Serialization
  • Check if a string to interpolate provides expected placeholders
  • Change an a tag attribute in JavaScript based on screen width
  • RestKit - RKRequestDelegate does not exist
  • Traverse Array and Display in markup
  • PHP: When would you need the self:: keyword?
  • Revoking OAuth Access Token Results in 404 Not Found
  • Acquiring multiple attributes from .xml file in c#
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • reshape alternating columns in less time and using less memory
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?