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 8 of 8
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help using GetElementById!

    I'm a student taking a CIS class, so I hope this isn't too basic of a question for ya.

    Anyways I'm trying to have an alert popup box come up when you don't enter your name in an input and keep that person from going to the next page.

    I can get an alert message to pop-up
    I can get it to keep form going to my next website
    I can get it to use if statement with vars i define in it like (var test = 0; if (test==0) {blah blah})
    But I can't get it to use firstname in the if statement.

    Heres part of my code for my Confirm.js:
    Code:
    // JScript File
    function valid()
    {
        var fname = document.getElementById("firstname");
       
        if (fname ==" ")
        {
            alert("You did not fill in your first name");
            return false;
        }
    }
    Heres part of my code for my Default.aspx:
    Code:
    <head>
              <script src="Confirm.js" type="text/javascript"></script>
    </head>
    
    <body>
            <form id="timeform" action="none.aspx" method="post">
                    First name:    <input type="text" name="firstname"/>   
                    <input type="submit" value="Clock In/Out" onclick="return valid()" />
             </form>
    </body>
    Is there something i'm doing wrong here?

    Thanks

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Well you have no id. You have to assign the element an id.



    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    name and id are different attributes. Your element has only an name, but no id.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    New Coder
    Join Date
    Oct 2008
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Code:
    <head>
              <script src="Confirm.js" type="text/javascript"></script>
    </head>
    
    <body>
            <form id="timeform" action="none.aspx" method="post">
                    First name:    <input type="text" id="firstname" name="other" />   
                    <input type="submit" value="Clock In/Out" onclick="return valid()" />
             </form>
    </body>
    Okay I did that, (actually i had tried that before), but it still doesn't work.....

    I'm using Visual Studio 2005 if that helps.

  • #5
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    865
    Thanks
    7
    Thanked 29 Times in 28 Posts
    Quote Originally Posted by metomeya View Post
    [CODE]
    I'm using Visual Studio 2005 if that helps.
    If you are using a MasterPage, sometimes the generated IDs of html tags are combined with those in the MasterPages.

    Even if you weren't using a MasterPage, I would look at the generated HTML to see if there are different from what you entered....just in case.

    Cheers
    ~E

  • #6
    New Coder
    Join Date
    Oct 2008
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Well, like I said, I really new to this programming stuff.

    There is the code behind the aspx file.

    Code:
    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    }
    Would that Page have anything to do with the master page?

    If so how do I link it all up?

  • #7
    New Coder
    Join Date
    Oct 2008
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Okay so I emailed the TA.

    I needed to make

    var fname = document.getElementById("firstname")

    into

    var fname = document.getElementById("firstname").value

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    asp (php, java, jsp...) has no relevance upon javascript (unless asp generates a javascript code, which is not important in this case). For javascript is important only the HTML generated document, thus you must analyze the HTML (what you see when View Source)
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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