0

I'm been trying on getting the xmlhttp.responseText after calling out the function as i wish to do some show and hide objects. But it seems that I cant match the innerhtml and show the object like button.

Updated Javascript function:

function ShowHideDisplay(str)
{
   xmlhttp = new XMLHttpRequest();
   //It will echo whatever message into this response.text.
   if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
   {
   document.getElementById("validate").innerHTML = xmlhttp.responseText;
   }
   //Using test
   var successText = "<img src=\"./images/success.gif\" alt=\"Correct!\">Can be used";
   document.getElementById("validate").innerHTML = responseText;
   if(document.getElementById("validate").innerHTML.test(/success/gmi))
   {
       document.getElementById("submit").style.visibility = 'visible';
   }
   else
   {
       document.getElementById("submit").style.visibility = 'hidden';
   }
}

HTML Form:

 <input name="Numbers" type="text" id="Numbers" onkeyup="ShowHideDisplay(this);" value=""/>
 <span id="validate"></span>
 //wants to hide and show upon onkeyup and getting the span id of validate.innerhtml success message
 <input name="submit" id="submit" type="submit">

I just need to get the span id of the validate value or innerhtml text to show and hide the button. But i try different ways yet it cant match the innerhtml text. Kindly advise.

2
  • Is your code considering that it needs to run the part that depends on the ajax call on a callback function to the ajax request unless it's making the call synchronously? Commented Jul 14, 2012 at 23:09
  • nv use ajax before. But is it possible to get the span id value ? so that i can say if it's success, the button will show Commented Jul 14, 2012 at 23:16

1 Answer 1

1

I created a Fiddle that probably does what you want.

The HTML

<input id="submit" type="submit" />
<span id="validate"></span>​

The JavaScript

/* Success */
var responseText = "<img src=\"./images/success.gif\" alt=\"Correct!\">Can be used";

/* Failure */
//var responseText = "<img src=\"./images/Failed.gif\" alt=\"Incorrect!\">Can not be used";

/* Assign content */    
document.getElementById("validate").innerHTML = responseText || "";

/* Change visibility based on outcome (success or not) */    
if(document.getElementById("validate").innerHTML.test(/success/gmi)) {
  document.getElementById("submit").style.visibility = 'visible';
} else {

    document.getElementById("submit").style.visibility = 'hidden';
}​

Test with a regular expression if the word 'success' was in the result or not. This way it doesn't really matter what the HTML in the response is.

Note There is no <success\> element in HTML, use a span or div with class success.

PS. If you can you should only return the word true or false or success or failure instead of HTML and render the text plus image on the client.

Sign up to request clarification or add additional context in comments.

4 Comments

make actual request? as in? Oh if you are referring to calling out another page. Yes, i succeeded. I can get the response.text. Now i want to know how to get the response.text in the span id
already change to getElementById. But still unable to do ===. Is it because my return message consists this ? ""<img src=\"./images/success.gif\" alt=\"Correct!\"> <Success>Can be used</Success>", PHP_EOL;
at this line: if(document.getElementById("validate").innerHTML.test(/success/gmi)). There's no test available. It doesn't call out..
how can u test(/success/gmi) ? in what condition ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.