Skip to content Skip to sidebar Skip to footer

How To Check If A Div Is Empty And Then Execute Action

I'm Building a Personal Assistant with HTML5, CSS, & Javascript This is what my current Problem is. I Have a
that contains scripts with the responses for when a Que

Solution 1:

Try this

<scripttype="text/javascript">
{
var q = getValue("q");
var name = getValue("name");
}
if (q == 'Whats+up') {
document.write("<div id='response_text'>"+"Not much " + name + "."+"</div>");
}
else {

}
</script>

And then:

<script>var theDiv = document.getElementById("response_text");
var q = getValue("q");
if(theDiv==null || theDiv.innerHTML == ''){
window.location = 'error.html?name=Brandon&q='+q;
}
</script>

That being said, there are probably better/more scalable ways of doing this with ajax.

Solution 2:

You could check, if there are no other elements than scripts in the div:

<script>var theDiv = document.getElementById('responce'),
        q = getValue('q'),
        scripts = theDiv.getElementsByTagName('SCRIPT').length;
    if (theDiv.children.length - scripts === 0) {
        window.location = 'error.html?name=Brandon&q=' + q;
    }
</script>

However, I'd suggested you to get rid of all inline scripts, and put them in the head of the document or just before the closing body tag instead.

EDIT

A quick-fix would be to move the script below the #responce.

Post a Comment for "How To Check If A Div Is Empty And Then Execute Action"