dcsimg
www.webdeveloper.com
Results 1 to 4 of 4

Thread: Problem with alert

  1. #1
    Join Date
    Sep 2003
    Location
    Utah
    Posts
    396

    Problem with alert

    My form has three text boxes named t1, t2 and t3. I created an array to loop through the boxes to make sure they have either a numeric entry or a zero. The function worked great while I had one generic alert. However, I would like to make the alert be more descriptive of the box. So, I created three alert messages named, msg1, msg2 and msg3. I figured as I looped through, this:

    alert(msg+i)

    would become:

    alert(msg1), alert(msg2), alert(msg3), but it doesn't. I think it might be trying to mathematically add msg and the value for i. The full function is below. Any ideas?


    function validateBox(){
    var msg1="Please enter a valid amount in box 1."
    var msg2="Please enter a valid amount in box 2."
    var msg3="Please enter a valid amount in box 3."
    for(i=1;i<4;i++){
    if((!Number(document.getElementById("t"+i).value)) && (document.getElementById("t"+i).value!="0")){
    alert(msg+i);
    document.getElementById("t"+i).value="";
    document.getElementById("t"+i).focus();
    document.getElementById("t"+i).style.backgroundColor="yellow";
    return false;}
    }
    }

  2. #2
    Join Date
    Dec 2004
    Posts
    8,637
    Move your three messages outside of the function so that you are not re-creating them every time the function executes. Then, you can change this:

    alert(msg+i);

    to this:

    alert(window["msg"+i]);

  3. #3
    Join Date
    Sep 2003
    Location
    Utah
    Posts
    396
    Very nice. Thank you. Would you mind elaborating a bit on what using "window" does?

  4. #4
    Join Date
    Dec 2004
    Posts
    8,637
    Basically, all variables declared outside of a function (at least, if they are done so in the HEAD section of the document) become global variables -- which are stored as properties of the window object. It is as simple as that.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles