2

I have an html page with the following element:

<span class="emojitext ellipsify" dir="ltr" data-reactid=".0.0:$main.4.3.0.0.0.$393382905560@c=1us.$393382905560@c=1us.0.1.1.0.$status"> How are you? </span> 

I'm trying to get it's content with this selector

message = $('span.emojitext').find('[data-reactid=".0.0:$main.4.3.0.0.0.$393382905560@c=1us.$393382905560@c=1us.0.1.1.0.$status"]').html(); console.log(message); 

But it returns undefined. At first sight my code looks correct, even by looking at other questions on SO, hence i cannot understand why it's not returning its content which should be How are you?

Any clue?

1
  • Use .filter instead of .find? Commented Mar 13, 2016 at 11:59

2 Answers 2

3

Your selector is incorrect as the span.emojitext is the element that has the attribute, therefore you don't need to use find():

message = $('span.emojitext[data-reactid=".0.0:$main.4.3.0.0.0.$393382905560@c=1us.$393382905560@c=1us.0.1.1.0.$status"]').html(); console.log(message); 

Working example

Sign up to request clarification or add additional context in comments.

Comments

0

Use .attr() instead off find()

message = jQuery('span.emojitext').attr('data-reactid','.0.0:$main.4.3.0.0.0.$393382905560@c=1us.$393382905560@c=1us.0.1.1.0.$status').html(); console.log(message); 

2 Comments

Calling .attr() would change the value of the attribute. It won't limit what elements are included in the set at all.
Why would you want to use the setter of attr()? The element already has that value. Your logic right now will return a string, which will then mean you get a syntax error when you call html().

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.