views:

123

answers:

3

Hi I'm trying to hide/show a div based on an AJAX response. I really don't know, is there way for that?

If response is an error, show the error text and show the div (same div).

If response is not an error, show the success text and hide a div (same div).

It's like facebook function.

I should do that with javascript function(s).

EDIT
Added information from OP's duplicate question:

There are my divs.

<div id="messages"></div>
<div id="edit-address-book">
codes to edit the address book , input type - text, select , textearea etc...
</div>

Down below there is one of my simple ajax requests. This is the ajax library , that I use -> http://en.dklab.ru/lib/JsHttpRequest/

 // Create new JsHttpRequest object.
 var req_update = new JsHttpRequest();
 // Code automatically called on load finishing.
 req_update.onreadystatechange = function() {
  if (req_update.readyState == 4) {
  document.getElementById("messages").innerHTML = req_update.responseJS.messages;
  document.getElementById("changed_address").value = req_update.responseJS.changed_address;
  }
 }

 document.getElementById("messages").innerHTML = req_update.responseJS.messages;

 req_update.responseJS.messages; // This is ajax respond part.

On the address book div, when customer click on update button there comes error or success message from ajax respond into messages div.

For Example:

    Error Messages:
    -----------------
    Your First Name must contain a minimum of 2 characters.
    Your Last Name must contain a minimum of 2 characters.
    Your Street Address must contain a minimum of 5 characters.
    Your City must contain a minimum of 3 characters.
    Your State must contain a minimum of 2 characters.

    Success Message:
    --------------------
    Your address book has been successfully updated. (

I've tried it simply down below, but it didn't work:

<script language="javascript" type="text/javascript">
if ( document.getElementById("messages").innerHTML == "<?php echo SUCCESS_MESSAGE; ?>" ) {
 hide_div('edit-address-book); //The div to hide the address book entries.
}
</script>

if there is a suggest/solution in jquery javascript library, I can use it as well.

A: 

This should do it.

EDITED

<body>

<script type="text/javascript">
    function teste(divId, text){
        var myDiv = document.getElementById(divId);

        if (myDiv.innerText.indexOf(text, 0) != -1) //could be replaced by any error veryfing logic
        {
            myDiv.style.visibility = "hidden";
            return 'Message 1';
        }
        else
        {
            myDiv.style.visibility = "visible";
            return 'Message 2';
        }
    }

</script>


<div id="MyDiv">blabla</div>

<script type="text/javascript">
    document.write(teste('MyDiv', 'blabla'));
</script>



</body>
born to hula
This only meets half of the requirements. It needs to be able to show the div as well.
Justin Johnson
At least I gave a starter, and you gave me -1? do you really think the answer is not useful? Why don't you post something useful instead of just whining?
born to hula
A: 

I really don't understand why it doesn't work with ajax respond. Down below codes are working on the static page. But when I try with ajax respond there is no result. What is wrong?

<!DOCTYPE html>
<html>
<head>
</head>

<script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
<script language="JavaScript" type="text/javascript">
 $(document).ready(function() {
    if ( $("#messages").text() == "Your address book has been successfully updated.") {
        $("#edit-address-book").hide();
    }
 });
</script>

<body>
    <div id="messages">Your address book has been successfully updated.</div>
    <div id="edit-address-book">What is wrong ?</div>
</body>
</html>
question_about_the_problem
A: 

Finally I've found the solution. It will help users, who are using the AJAX library "JSHttpRequest" ( http://en.dklab.ru/lib/JsHttpRequest/ ). Here it is:

Find:

    document.getElementById("messages").innerHTML = req_update.responseJS.messages;
    document.getElementById("changed_address").value = req_update.responseJS.changed_address;

###Replace with:

    document.getElementById("messages").innerHTML = req_update.responseJS.messages;

    if (req_update.responseJS.messages == "<?php echo SUCCESS_MESSAGE; ?>") {
        hide_div("edit-address-book");
    }

    document.getElementById("changed_address").value = req_update.responseJS.changed_address;
    }
question_about_the_problem