Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Selecting text using nth-of-type returns concatenated string

I have the following HTML and I want to select the value "1933".

I tried $('div.mydata dl:nth-of-type(1)').text() but that returns "1933110 m243" where I’d expect "1933". What am I doing wrong?
I checked here already.

console.log($('div.mydata dl:nth-of-type(1)').text());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="mydata">
  <div>
    <div class="left">
      Year: Lot size: Total: Cars: Motors:
    </div>
    <div class="right">
      <div>
        <dl>1933</dl>
      </div>
      <div>
        <dl>110 m<sup>2</sup></dl>
      </div>
      <div>
        N.A.
      </div>
      <div>
        <dl>4</dl>
      </div>
      <div>
        <dl>3</dl>
      </div>
    </div>
  </div>
</div>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The selector: $('div.mydata dl:nth-of-type(1)').text() is essentially selecting the first of every <dl> inside of div.mydata.

If you just want the contents of the first <dl> on the page use:

$($('div.mydata div dl').get(0)).text()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading