Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7689975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:17:24+00:00 2026-05-31T20:17:24+00:00

var playerClass = $(this).parent().attr(‘class’) Question about only selecting a specific class from the dom.

  • 0
var playerClass = $(this).parent().attr('class')

Question about only selecting a specific class from the dom.

What can I add to this line to only select the class that has a class of player-1, player-2, basically anything with a prefix of player- followed by a number?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:17:26+00:00Added an answer on May 31, 2026 at 8:17 pm

    I couldn’t tell exactly what you were asking with your question, so here are four possibilities. Hopefully one of these matches what you intended to ask or you can slightly modify one of them to fit.

    To select all input items with a class of player-xx where xx is a number, you can do this:

    var playerClass = $('input[class^="player-"]').filter(function() {
        return((" " + this.className + " ").match(/\splayer-\d+\s/) != null);
    });
    

    The first jQuery selector gets any class name that starts with "player-". The filter then eliminates any items that aren’t also "player-nn".

    If you’re only trying to examine whether one particular classname contains player-nn, then you can do that like this:

    if ((" " + $(this).parent().attr('class') + " ").match(/\splayer-\d+\s/) != null) {
        // the parent has player-nn as a class
    }
    

    If you just want to get the matching class on the parent, you can do that with this:

    var matches = (" " + $(this).parent().attr('class') + " ").match(/\s(player-\d+)\s/);
    if (matches) {
        playerClass = matches[1];
    }
    

    If you’re trying to actually get the number after the player-, you can do that like this:

    var matches = (" " + $(this).parent().attr('class') + " ").match(/\splayer-(\d+)\s/);
    if (matches) {
        playerNum = parseInt(matches[1], 10);
    }
    

    Note: All of these examples use a trick (which I learned from jQuery) that if you add a space onto the beginning and end of the overall classname, then you can look for a given classname with a regex by looking for your particular classname surround on both sides by whitespace. This makes sure you don’t match a part of a longer classname and only match whole classnames. jQuery uses this trick in it’s .hasClass() implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

var currentClassName = $(this).parent('td').next('td').find('input:text').attr('class'); if (currentClassName == undefined) { currentClassName = $(this).parent('td').find('input:text').attr('class'); } This
var classes = $(this).attr('class').split(' '); // this gets the current element classes var classes
var auditAgencyRecords = (from ag in db.Agencies join ara in db.AuditRuleAccounts on ag.Agency_Id equals
var $imagefile = $('<input />') .attr({ type: 'file', name: 'imageFile', id: 'imageFile' }); Above
This is my ViewModel: public class PlayerViewModel { PlayerRepository repo = new PlayerRepository(); public
I created this viewmodel: public class PlayerViewModel { PlayerRepository repo = new PlayerRepository(); public
var q = 'SELECT * FROM stories_comments WHERE story_id=? AND user_id=? ORDER BY created_at
var t={a:1,b:2} is this an Object with properties (a,b) or it is a Javascript
var valueAsText = $('#counts span').text(); I use this tiny script to parse a span
var query = from C in db.clients join O in db.orders on c.clientid equals

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.