1

I tried searching the related posts, and having a hard time figuring out how to fix my query - I'm pretty close, any help is much appreciated (new to Jquery).

I program in PHP, and trying to pull either the HREF value from a tag, or the text. Either will work.

I basically have my HTML code in a string, might contain multiple tags, and would like to load the text of the tags into either a PHP array or variable (right now just trying to ALERT the results, I can dump it later).

My PHP Code:

<?php
$info = '<li><strong><a href="http://www.mysite.com/test.html" title="Some stuff">I want this text</a></strong>';

echo '<script type="text/javascript">';
echo '$("document").ready( function () {';
echo 'alert($("a", $("' . $info . '")).html());';
echo '});';
echo '</script>';
?>

The above doesn't alert anything. Putting in

echo 'alert("yes")'; 

does work, so I'm guessing there's something basic wrong with my syntax, but 4 hours later still unable to find it! :)

Thanks in advance.

7 Answers 7

5

You aren't Javascript-escaping the quotes in your string.

Your code creates Javascript that looks like

$("<li>...<a href="http..."...")

The quotes in the attribute end the Javascript string, creating a syntax error.

You need to call json_encode.

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

6 Comments

json_encode($info); it adds slashes but still doesn't look right
<script type="text/javascript"> $(document).ready( function () { alert($("a", $(""<li><strong><a href=\"http:\/\/www.mysite.com\/test.html\" title=\"Some stuff\">I want this text<\/a><\/strong><\/li>"")).html()); }); </script>
Bingo. Works. And if my HTML string has multiple <a> tags and I want to get the .each, then i dump .each at the end of my jquery? like this: alert($("a", $(<?php echo $info; ?>)).html().each); ?
No. You need to pass an anonymous function to .each which calls alert($(this).html()).
Cool thanks. I gotta run out of the office but will add that when I get back. Thanks for the help.
|
3

SLaks has the rest of your problem. But also, it's not:

$("document").ready();

It's:

$(document).ready();

The former is a selector for a tag named <document>.

Comments

2

This should work the way you want it to:

<?php
$info = '<li><strong><a href="http://www.mysite.com/test.html" title="Some stuff">I want this text</a></strong></li>';
?>

<script type="text/javascript">
    $(document).ready( function () {
        alert($("a", $("<?php echo $info; ?>")).html());
    });
</script>

1 Comment

Thanks - tried the above - still not giving me an alert though. My code, in code view, looks weird like i'm still not enclosing correctly.
1

You are not closing your li Tag

$info = '<li><strong><a href="http://www.mysite.com/test.html" title="Some stuff">I want this text</a></strong>';

should be

$info = '<li><strong><a href="http://www.mysite.com/test.html" title="Some stuff">I want this text</a></strong></li>';

Comments

1

You should escape info. It's breaking because you've got double quotes inside of double quotes:

$info = addslashes($info);

or

$info = json_encode($info);

or just

$info = str_replace('"', '\\"');

1 Comment

Thanks for the input - did the middle suggestion about json_encode($info) but it drops slashes into my HMTL output as well, must be that my host has magic_quotes on or something.
0

Try to save the HTML is a JS variable first, then use it. Also, heredocs are your friend.

<?php
$info = '<li><strong><a href="http://www.mysite.com/test.html" title="Some stuff">I want this text</a></strong></li>';

echo <<<END
<script type="text/javascript">
$(function(){
  var HTML = '$info';
  alert($('a', $(HTML)).html());
});
</script>
END;
?>

Comments

0
echo '<script type="text/javascript">
    $(document).ready( function () {
        var info = \''.$info.'\';
        $("a").html(info);
        alert(info);
    });
</script>';

1 Comment

that's close, almost works - it alerts the entire info string - if i just want the text in the <a> tags, the jquery needs to be where? in the alert?

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.