For example I’m trying to select the p tag inside of .col four hide-mobile on this page:
http://www.sportsline.com/college-basketball/odds/
In the console I tried using $(“col four hide-mobile”,”p”).css(“color”,”green”); to change the p tag in that row to green, but I’m screwing something up. Any idea on how I can do this to get the data from that row?
<div class="col four hide-mobile">
</div>
</div>
<div class="row-group" itemscope itemtype="http://schema.org/SportsEvent">
<meta itemprop="name" content="Penn St. at Indiana">
<div class="seo location" itemprop="location" itemscope itemtype="http://schema.org/Place">
<meta itemprop="name" content="Assembly Hall">
<div class="seo location-address" itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
<meta itemprop="addressLocality" content="Bloomington">
</div>
</div>
<a id="ember1528" href="/college-basketball/game-forecast/NCAAB_20170201_PSU@IND/" class="row data-row ember-view">
<div class="col one">
<div class="container">
<div class="top" itemprop="startDate" content="2017-02-01T18:30:00-05:00">
<i class="icon ncaab"></i> 6:30 PM EST
</div>
<div class="middle">
<span itemprop="awayTeam">PSU</span>
<span class="odd">
<!----></span>
</div>
<div class="bottom">
@
<span itemprop="homeTeam">Indiana</span>
<span class="odd">
<!----></span>
</div>
</div>
</div>
<div class="col two hide-mobile">
<div class="container">
<p> IND <i class="change-icon down"></i> </p>
<p>-265</p>
</div>
</div>
<div class="col three hide-mobile">
<div class="container">
<p> O/U <i class="change-icon down"></i> </p>
<p>+144.5</p>
</div>
</div>
<div class="col four hide-mobile">
<div class="container">
<p> IND <i class="change-icon down"></i> </p>
<p>-6 (-110)</p>
</div>
</div> </a>
</div>
<div class="row-group" itemscope itemtype="http://schema.org/SportsEvent">
<meta itemprop="name" content="George Mason at Saint Louis">
<div class="seo location" itemprop="location" itemscope itemtype="http://schema.org/Place">
<meta itemprop="name" content="Chaifetz Arena">
<div class="seo location-address" itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
<meta itemprop="addressLocality" content="St. Louis">
</div>
</div>
<a id="ember1573" href="/college-basketball/game-forecast/NCAAB_20170201_GMASON@STLOU/" class="row data-row ember-view">
<div class="col one">
<div class="container">
<div class="top" itemprop="startDate" content="2017-02-01T19:00:00-05:00">
<i class="icon ncaab"></i> 7:00 PM EST
</div>
<div class="middle">
<span itemprop="awayTeam">GMASON</span>
<span class="odd">
<!----></span>
</div>
<div class="bottom">
@
<span itemprop="homeTeam">STLOU</span>
<span class="odd">
<!----></span>
</div>
</div>
</div>
<div class="col two hide-mobile">
<div class="container">
<p> GMASON <i class="change-icon up"></i> </p>
<p>-333</p>
</div>
</div>
<div class="col three hide-mobile">
<div class="container">
<p> O/U <i class="change-icon down"></i> </p>
<p>+132.5</p>
</div>
</div>
3
Answers
Given the HTML structure you have, you can use the descendant selector (which is a space) along with the
nth-child()
pseudo-class selector to select the second<p>
element within the<div>
in question:And, to extract the numbers from that element, we need to do some string parsing:
Learn about selectors here.
Since .col, .four, and .hide-mobile are all classes you need to write your descendent selector like this
There’s a few ways to do this. You can include it as a descendant in the selector as @ScottMarcus mentioned:
Or you could use
find()
to grab it from the parent:I would personally use the first since using an extra jquery selector is a bit of overkill, but there are times when it’s necessary to use the second (for example if
p
is dynamically inserted into the DOM at a later date and you’ve already got a reference to thecol
) so I thought I’d chime in anyway