Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Naming an alert

  1. #1
    New Coder
    Join Date
    Sep 2005
    Location
    Illinois
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Naming an alert

    Ok i want to name an alert so that It can be called upon without being in a function. Code is as follows:

    Code:
    <html>
    <head>
    <title>
    Sign In page
    </title>
    
    <script type="text/javascript">
    
    function AccessText()
    
    {
    return document.getElementById("FirstName")
    }
    
    </script>
    
    <script type="text/javascript">
    
    {
    window.alert("Welcome, "+document.getElementById("FirstName")+" you are signed in")
    }
    
    </script>
    
    </head>
    
    <body>
    
    <form>
    <input type="text" id="FirstName" value="Enter your first name">
    <input type="button" value="Sign In" onclick="">
    </form>
    
    </body>
    
    </html>
    OK so how would I assign the window.alert a name?? So that I can access it with the onclick feature? Thank you.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    You can't. OK?

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Why not simply go

    onclick="alert('Hello World')"

    or put it in a function

    function myClick(){

    alert("Hello World)

    }

    onclick="myClick()"
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #4
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Edit: Posted at same time as MrJ!

    no need to name alerts - it is native code of all javascript engines. Also there's no need to call an alert explicitly through the window object:
    Code:
    window.alert("hello world");
    Can be referenced more cleanly as:
    Code:
    alert("hello world");
    You can drop the call directly into your onclick event (note the use of single quote marks within the html onclick attribute):
    Code:
    <input type="button" value="Sign In" onclick="alert('Welcome '+document.getElementById('FirstName')+' you are signed in');">
    This is much better run as a function however as validation is needed:
    Code:
    function doAlert() {
      var el = document.getElementById("FirstName");
      if(el.value.length == 0) {
        alert("Please enter your first name");
      } else {
        alert("Welcome "+el.value+" you are signed in");
      }
    }
    
    // Your html button:
    <input type="button" value="Sign In" onclick="doAlert();">
    Hope this helps

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper


  •  

    Posting Permissions

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