Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse HTML from an AJAX request?

I am currently trying to get a value from a form in HTML, and I get the HTML by doing a GET request to a url, and then parse that HTML using jquery.

Here is my HTML that I am trying to parse through:

<!doctype html>
<title>Test</title>
<body>

    <form name="guestform">
        <input type="hidden" name="ip" value="198.168.123">
    </form>
</body>

Here is the jquery I use to get the HTML and then try to parse it:

$.ajax({
    type : 'GET',
    url: "http://localhost:8000/test.html",
    success: function(data){
        var form = $(data);
        var ip = $("input[name='ip']").find(form).val();
        alert(ip);
    }

});
like image 959
user1457388 Avatar asked Jan 25 '26 06:01

user1457388


2 Answers

The input element is within the form element so you need to call form.find(.....)

$.ajax({
    type: 'GET',
    url: "http://localhost:8000/test.html",
    success: function (data) {
        var form = $(data);
        var ip = form.find("input[name='ip']").val();
        alert(ip);
    }

});
like image 88
Arun P Johny Avatar answered Jan 27 '26 21:01

Arun P Johny


You are searching your main document for an input and then trying to find the HTML document you loaded via Ajax inside it. You want to reverse that.

$("input[name='ip']").find(form)

should be

form.find("input[name='ip']")
like image 36
Quentin Avatar answered Jan 27 '26 20:01

Quentin