0

i have a file that gets its content from another file, i use JS to call a php file that gets information from a database and display it.

This works fine, but i want to be able to get hold of the information that has been displayed from the other file, i thought i could use $('.className').val(); but this always seens to return 'undefined'.

this is the JS code so far: index.php

$(document).ready(function() 
{
    $(".trade_window").load('signals.php?action=init');
    console.log($('.market_name_1').val());
});

As i said the output of the init is just fine but getting hold of the value is the problem, this is the HTML that it generated by the php file :

<div class="market">
    <h3 class="market_name_1">GBPUSD</h3>
    <img src="images/buy.png">                                              
    <p class="market_data">
    </p><p>BUY</p>
    <p class="market_data">1.55605</p>
    <p class="market_data">18:21:02</p> 
</div>

I am currently learning the wonders of JS and JQuery so looking to learn from my mistakes. Any ideas on this one? I really think i'm just missing something silly but i can't but my finger on it.

3 Answers 3

4

As Zenith mentioned: use text() to retrieve the value from the heading.

You might get undefined even after using text() if the data hasn't been loaded by the time the statement is executed. You can use the callback provided for the load() function.

$(document).ready(function() 
{
    $(".trade_window").load('signals.php?action=init', function() {
        console.log($('.market_name_1').text());
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

@Zenith I've also added the callback function that wasn't mentioned in the other answers, so I posted rather than up-voting.
This works, i can output the value to the console, the only problem is it has lots of additional space around it. Now i've had this problem before and it was the fact that i had space at the end of my php file but this php file is set out differently is instead of echoing things out i use a number of if statements like this <?php if(something) : ?> any ideas? i can add the php code if you need it.
You can use the String.trim function provided by JavaScript to remove leading and trailing spaces.
3

Use jQuery's text() method instead to get the value from inside:

console.log($('.market_name_1').text());

You may also want to use an on('load') instead of document.ready.

Comments

1

Try accessing the the value you need inside the callback function specified for the jquery load() method. There is a complete method that can be called when the data from the server is retrieved.

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.