qadenza
qadenza

Reputation: 9293

How to get div containing some text

HTML:

<div class="linkL 01 mp3/01.mp3 imgT/03.jpg">lorem1 - part1</div>
<div class="linkL 02 mp3/02.mp3 imgT/03.jpg">lorem2 - part2</div>
<div class="linkL 03 mp3/03.mp3 imgT/03.jpg">lorem3 - part3</div>

Javascript:

var numera = window.location.hash;
var numera  = numera.replace("#","");
console.log(numera); // `part3`
var author = $(".linkL:contains(numera)").html().split(" - ")[0];  

I would like to have the result of lorem3

But this is what the console says:

index.js:37 Uncaught TypeError: Cannot read property 'split' of undefined

Upvotes: 3

Views: 64

Answers (2)

jeffld
jeffld

Reputation: 726

If you need to find lorem3, then you could loop through each of the divs looking for it.

  $(".linkL:contains(" + numera + ")").each(function() {
var author = $(this).text().split(" - ")[0];
if (author == "lorem3") {
  $(".result").text($(this).attr('class'));
}});

jsfiddle example

Upvotes: 1

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67217

The way that you are concatenating the value is wrong,

var author = $(".linkL:contains(" + numera + ")").text().split(" - ")[0]; 

You have to concatenate the variable using +, If you don't then that variable name would be considered as a plain string. That value it holds will not be concatenated.

And the class name that you are using mp3/01.mp3 is invalid. A valid class name should follow this rules.

Upvotes: 5

Related Questions