logo

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ
No Result
View All Result
logo
No Result
View All Result
Trang chủ javascript array indexof() method

Javascript Array Indexof() Method

by Admin _ June 13, 2022
I"ve got multiple music players on a page and need to make an index of them to pull the position of the current player from. The issue is that the currentPlayer is not a child, so using .find or .filter and then .index will always return a value of 0 because nothing else is in the array.

Bạn đang xem: Javascript array indexof() method

So I need to find .currentPlayer"s index within the player array.

HTML (very simplified):


JavaScript:

var player = $(".player"), current = player.filter(".currentPlayer"), index = current.index();

*

current.index() will search its parent for the element. so since current is an only child, it"s zero.

Xem thêm: ' Bharal Là Gì - Cừu Bharal: Loài Động Vật Có Vú

You can pass a selector to .index; it"ll tell jQuery to search inside that for your element.

var player = $(".player"), current = player.filter(".currentPlayer"), index = current.index(".player");Or, you can tell jQuery to search for a specific element inside of an array:

var player = $(".player"), current = player.filter(".currentPlayer"), index = player.index(current);

*

You"re probably looking for

$("div.player").index($(".currentPlayer"))http://jsfiddle.net/hmartiro/3Wzbd/


*

var position = 0;$(".player").each(function(i){if ($(this).hasClass("currentPlayer") == true) { position = i; }});
*

Thanks for contributing an answer to Stack Overflow!

Please be sure to answer the question. Provide details and share your research!

But avoid …

Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.


Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy


Not the answer you're looking for? Browse other questions tagged jquery arrays class filter or ask your own question.


*

Site design / logo © 2022 Stack Exchange Inc; user contributions licensed under cc by-sa. rev2022.6.10.42345


Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

ShareTweet Linkedin Pinterest
Previous Post

Kiểm tra form hợp lệ với jquery

Next Post

CÙNG CHUYÊN MỤC

jquery file upload (hayageek plugin) error case with json from php

Jquery file upload (hayageek plugin) error case with json from php

28/04/2021
search/filter table rows based on input

Search/filter table rows based on input

28/04/2021
jquery form validation before ajax submit

Jquery form validation before ajax submit

18/03/2021
jquery multiple select dropdown

Jquery multiple select dropdown

28/04/2021
how to get element by class name

How to get element by class name

21/06/2022
upload file with web service (asmx) using jquery ajax in asp

Upload file with web service (asmx) using jquery ajax in asp

20/06/2022
javascript array indexof() method

Javascript array indexof() method

13/06/2022
hiệu ứng zoom ảnh jquery

Hiệu ứng zoom ảnh jquery

11/06/2022

Newsletter

The most important automotive news and events of the day

We won't spam you. Pinky swear.

Chuyên Mục

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ

News Post

  • Phần mềm dpt

About

Chúng tôi tạo ra trang web nhằm mục đích mang lại kiến thức bổ ích cho cộng đồng, các bài viết được sưu tầm từ nhiều nguồn trên internet giúp mang lại kiến thức khách quan dành cho bạn

©2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng

Liên Hệ - Giới Thiệu - Nội Quy - Bảo Mật

No Result
View All Result
  • Trang chủ
  • Chuyên mục
    • Hỏi Đáp
    • Kiến Thức
    • Sức Khỏe
    • Tử Vi
    • Công Nghệ
  • Lưu trữ
  • Liên hệ

© 2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng.