Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

329
Views
Why do jQuery .prev() and .next() work incorrectly?

Why when I use .prev() (the same goes with .next()) here to launch all audios except the one before r2, it starts all except r2 and some random one (not exact previous)? It should play only the previous to the r2 element. I try to understand this

<html>
<head>
<script type="text/javascript" src="RadioN.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#i1").click(function(){
var player = $("audio").not("#r2").prev();for(i=0; i<player.length; i++) player[i].play();
});
});
</script>
</head>
<body>
<a href="#">
<img id="i1" src="r1.png" style="height:50px"></a><br>
<div>
<audio class="r" id="r1" src="https://rs103-krk-cyfronet.rmfstream.pl/RMF24" controls preload="none" style="visibility: visible"></audio>
<audio class="r" id="r2" src="https://radiostream.pl/tuba10-1.mp3" controls preload="none" style="visibility: visible"></audio>
<audio class="r" id="r4" src="https://c13.radioboss.fm:18296/stream" controls preload="none" style="visibility: visible"></audio>
<audio class="r" id="r5" src="http://stream.affordablestreaming.com:8000/KDDL.mp3" controls preload="none" style="visibility: visible"></audio>
<audio class="r" id="r6" src="https://p10.p4groupaudio.com/P10_MM" controls preload="none" style="visibility: visible"></audio>
</div>
</body>
</html>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

jQuery's prev method will return the array of previous siblings of the elements matching .not('#r2').

.not('#r2') will return [audio#r1.r, audio#r4.r, audio#r5.r, audio#r6.r].

.not('#r2').prev() will return [audio#r2.r, audio#r4.r, audio#r5.r]. This is :

  • #r1 previous sibling will return none
  • #r4 previous sibling will be #r2
  • #r5 previous sibling will be #r4
  • #r6 previous sibling will be #r5

If what you want is to play only the previous sibling to #r2 you should use a more deterministic selector. something like $("audio#r2").prev().get(0).play() may be?

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!