Click here to Skip to main content
16,018,818 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am trying to use javascript to search the html text on a website. In order to do this I am using the dom tree. i wrote code to look at the html contained in a h4 element, and i can see that it found all 6 headers(d.length alert), and using chrome, i can see the textContext of each is what i would expect, but i cannot figure out the syntax to access the text of the h4 element(using last alert). PLEASE HELP!!!!! (3rd alert only accesses text of first h4 element)
JavaScript
$(document).ready(function (e) {
        $('#submit').click(function (e) {
            try {
                var $inputTxt = $('#search');
                result = $inputTxt.val();
                alert(result);
                var d = $('h4');
                alert("load html");

                for (var i = 0; i < d.length; i++) {
                    alert(d.length);
                    alert($('h4').html());
                    alert($d[i].html().textContext);
                }
                if (d == result) {
                    alert("result found");
                } alert("got to end");
            } catch (ex) { e.preventDefault(); }
        });
Posted
Updated 20-Sep-12 8:49am
v3

I believe you need to use .text(), not .html() to get this value.
 
Share this answer
 
C#
$('h4').each(function(index) {
    alert(index + ': ' + $(this).text());
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900