0

I am trying to get parse HTML document.

this is the HTML:

<h1>
<span class="memName fn" itemprop="name">Ankur Arora</span>
<span class="display-none" itemprop="image">http://photos1.meetupstatic.com/photos/member/3/8/f/8/member_249974584.jpeg</span>
<span class="display-none" itemprop="url">http://www.meetup.com/Meetup-API-Testing/members/191523682/</span>
</h1>

I need to get the picture and the name.

I try this code:

var name = document.querySelector("memName fn").name;

Anyone can help me? I'm new in javaScript...

Thanks

2
  • Would you like to get the inner text of the targetted elements? Commented Oct 25, 2015 at 12:39
  • @JeroenBellemans yes!! Commented Oct 25, 2015 at 12:39

3 Answers 3

1

To get the inner text, you can use the text() function, like this:

HTML:

<span class="memName fn">Ankur Arora</span>

Jquery:

var memName = $(".memName").text();
console.log(memName); // Via console log
alert(memName); // Alert it
Sign up to request clarification or add additional context in comments.

4 Comments

Hello Jeroen Bellemans, thans for your answer... please see the comment in the answer of Chris Breaks...
So what exactly is the problem then?
there is possible without jquery?
1

It's easy with jQuery. Just include it in your page:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>

Then use .text() or .html() to extract the content of the span-elements

var pictureLink = $("span[itemprop='image']").text();
//.html() also gets the html-elements inside
var name = $("span[itemprop='name']").html();

https://jsfiddle.net/bh9mebru/

3 Comments

Hello Chris Breaks, thanks for your answer, but its not working... first I need to search the class in the page no??
I can't think of a way why this wouldn't be working (see the jsfiddle-link). Do you get javascript-errors? Generally speaking: You can address elements in different ways. for example by id, by classname, by position relative to other elements, or in this case by your unique attribute itemprop. a good way to debug your script is by passing out data with console.log("data") at vital positions.
there is possible without jquery?
0

You can also use innerHTML to get the text.

<span id="memId" class="memName fn">Ankur Arora</span>

document.getElementsByClassName('memName') - This will give the list of elements having the class 'memName'

To get the first element's inner text use document.getElementsByClassName('memName')[0].innerHTML

or access by id .

document.getElementById('memId').innerHTML

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.