2

I send a request with AJAX to retrieve Html from a page like this :

 function getHTML() {
        //set ajax call
        var options = {
            url: '/Controller',
            type: 'GET',
            dataType: 'html'
        }
        //make call
        return $.ajax(options).then(querySucceded).fail(queryFailed);

        //handle theajax callback
        function querySucceded(data) {
            console.log(data);
            //THE NEXT LINE IS THE PROBLEME
            var val = data.getElementByName("somename").val();
        }

        function queryFailed(jqXHR, textStatus) {
            //var msg = 'Error getting data. ' + textStatus;
            console.log('Error while getting data');
        }
    }

so the ajax call works great and the querySucceded function is called, and the data is retrieved correctly.

but the data is considered as a string. how can I manipulate the DOM inside the data object using jquery, like:

$("somename").val();
joshua
  • 2,371
  • 2
  • 29
  • 58

3 Answers3

0

getElementByName is available on the document object

Also it is supposed to be getElementsByName . And not on the response HTML.

Sushanth --
  • 55,259
  • 9
  • 66
  • 105
0

Actually, you can use some jQuery magic to get a DOM structure from a long html string:

$("<div/>").html(data).contents();

I'm not sure, but I think that $(data) directly will return an array of nodes instead.

sixFingers
  • 1,285
  • 8
  • 13
0

Try this:

var $data = $(data),
var value = $data.filter("your selector here").val()

console.log(value)
Engineer
  • 5,911
  • 4
  • 31
  • 58