916 Networks
916 Networks

Reputation: 545

Jquery to parse HTML in a string

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.

Upvotes: 1

Views: 784

Answers (7)

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

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

Upvotes: 0

gen_Eric
gen_Eric

Reputation: 227190

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;
?>

Upvotes: 0

Jeroen
Jeroen

Reputation: 13257

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>

Upvotes: 2

dtbarne
dtbarne

Reputation: 8190

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('"', '\\"');

Upvotes: 1

Robert Beuligmann
Robert Beuligmann

Reputation: 1444

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>';

Upvotes: 1

Jason McCreary
Jason McCreary

Reputation: 72961

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>.

Upvotes: 3

SLaks
SLaks

Reputation: 887225

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.

Upvotes: 5

Related Questions