1

I have been looking around for answer to this question for a bit, but I cant seem to find anything that helps. I would like to say first that I am relatively new to javascript/jQuery/Web Development in general (I come from more of a software background).

I am trying to get text from another page to display on my home page, or even use the text as a variable. Here is what I have so far, but I am not getting the results I am looking for.

Page 1 (the home page, just the jQuery, trying to output the return in console first)

$(function(){
$.get('Page2.html', function(result){
var obj = $(result).find('body');
    var PageText = $(result).find('testJQ').text();
    console.log(PageText);
});
});

Page2 (The page with the info I would like to grab)

<body>
<p id="testJQ">This is the text I want</p>
</body>

I get an undefined from the console, but I am unsure why. Like I said, I am fairly new with jQuery, and dont know exacly what is going on. These sites are in the same domain, so there shouldnt be any security issues grabbing information from one another.

1
  • 1
    you are missing # for id, var PageText = $(result).find('#testJQ').text(); Commented Sep 24, 2014 at 18:04

3 Answers 3

1

Try to change find('testJQ') to find('#testJQ'). I added hash character for id.

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

Comments

1

The simpliest way to do this is:

$(function(){
    $('#text').load('page2.html #testJQ');
});

Greetings

3 Comments

Thank you, but what would #text be
#text is just a DIV where the content will be stored in! <div id="text"></div>
Oh wow, that is nice abbreviation of what I was doing. Thanks for that tip!
0

On the id field, you need the # character.

Use

var PageText = $(result).find('#testJQ').text();

1 Comment

Oh my gosh, my syntax! Thank you so much, that worked perfectly!

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.