Question

In following example of code, I want to traverse the responseText object which consist the html code came from request_page.php file. In onSuccess event, i want to check whether < Div > with id 'ersDiv' has any errors posted in it.

new Request.HTML({
    url: 'request_page.php',
    onSuccess: function(responseText, responseXML) {
    // My expected code to handle responseText object
    alert(errorMessage);
    },
    onFailure: function() {  }
});

request_page.php file is like this :

<div align='center'><div id='ersDiv'>Page loaded with insufficient data</div></div>
Was it helpful?

Solution

try this for 1.2.x (example tweaked for the jsfiddle api):

new Request.HTML({
    url: '/echo/html/',
    method: "post",
    data: {
        html: "<div align='center'><div id='ersDiv'>Page loaded with insufficient data</div></div>",
        delay: 1
    },
    onComplete: function(responseText, responseXML) {
        var error, errors = responseXML.getElements("div").filter(function(el) {
            return el.get("id") == "ersDiv";
        });

        if (errors.length) {
            error = errors[0].get("text");
            alert(error);
        }
    }
}).send();

working example:

http://www.jsfiddle.net/dimitar/vLgqB/

in 1.3 this can work as oskar suggested:

console.log($$(this.response.tree).getElement("#ersDiv")[0].get("text"));

http://www.jsfiddle.net/dimitar/vLgqB/2/

have fun.

OTHER TIPS

function(responseText, responseXML) {
  if (responseText.getElement('#ersDiv').get('text') === 'Page loaded with insufficient data'){
    console.log('There was an error');
  }
}

Btw. 1990 called, they want their align='center''s back :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top