learning Jquery and integrating with PHP – getting there, but have one last challenge in some code I’m working on.
I have HTML in a string, trying to pull html in tags, might be multiple elements in the HTML string, so trying to use each. My function worked fine without each, below is my each integration (returns nothing currently):
<?php
$info = '<li><strong><a href="http://www.mysite.com/test.html" title="Some stuff">I want this text</a></strong></li><li><strong><a href="http://www.mysite.com/test2.html" title="Some stuff">I want this text too</a></strong></li>';
$info = json_encode($info);
?>
<script type="text/javascript">
$(document).ready(function () {
$("a", $( < ? php echo $info; ? > )).each(
function () {
alert($(this).html());
});
};
This code below does work, but only returns the first element in the HTML:
<?php
$info = '<li><strong><a href="http://www.mysite.com/test.html" title="Some stuff">I want this text</a></strong></li>';
$info = json_encode($info);
?>
<script type="text/javascript">
$(document).ready(function () {
var output = $("a", $( < ? php echo $info; ? > )).html();
var link = $("a", $( < ? php echo $info; ? > )).attr("href");
alert(output);
alert(link);
});
</script>
This is a description and a working example of How to use
.each()LINKYou can try this one as a example