how to get html textbox values into javascript variables

Posted on 2012-08-22
Last Modified: 2012-08-22
I am not sure what I am doing wrong here, but I have a super simple HTML page that has txt boxes for first name and last name. I would like to get the text in those text boxes and put that into a javascript variable. My end goal would be to write this data to a SQL server database table. However for now, I would like to just figure out how to "alert" the variables after they have been populated. My code is below:

    Put comments here
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Data Entry</title>

    <script type="text/javascript">
        function getTextValues()
    var fname = document.getElementById('txtFname').value;
    var lname = document.getElementById('txtLname').value;


    First name:<input value="" type="text" id="txtFname" name="firstname" />
    Last name:<input value="" type="text" id="txtLname" name="lastname" />
        <button onclick="getTextValues()">CLICK ME</button>

Open in new window

Question by:sqlagent007
    LVL 58

    Accepted Solution

    Working fine here on IE9 and Firefox and Chrome
    Is that really all the code on the page.
    LVL 1

    Author Closing Comment

    Apparently I had to allow the "blocked content".

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This is an explanation of a simple data model to help parse a JSON feed
    JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now