www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Simple Script NOT Working!

  1. #1
    Join Date
    Feb 2012
    Location
    Bokaro Steel City (Jharkhand), India!
    Posts
    194

    resolved [RESOLVED] Simple Script NOT Working!

    Dear Friends,

    Please help me out! The following script does not work:

    Code:
    <input id="_text" size="60" />
    <button onclick="_mySubmit()">Submit</button>
    <p>You entered: <output id='_output'></output></p>
    
    
    <script>
        var text = document.getElementById('_text').value;    
        var _mySubmit = function () {
            if (text == "") {
                alert('Please enter some text!');
            } else {
                document.getElementById('_output').innerHTML = text;
            }
        }
    </script>
    Any help on this would be appreciated!

    Thanks!
    "It will never rain roses: when we want to have more roses, we must plant more roses." - George Eliot

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    862
    Your code is setting the 'text' variable as the page loads. Since the input is empty when the page loads, the text variable is always an empty string. You need to set the 'text' variable in the function so it updates each time you click your button.

    Code:
    <input id="_text" size="60" />
    <button onclick="_mySubmit()">Submit</button>
    <p>You entered: <output id='_output'></output></p>
    
    <script>
    	var _mySubmit = function() {
    		var text = document.getElementById('_text').value;    
    		if (text == "") {
    			alert('Please enter some text!');
    		} else {
    			document.getElementById('_output').innerHTML = text;
    		}
    	}
    </script>
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  3. #3
    Join Date
    Feb 2012
    Location
    Bokaro Steel City (Jharkhand), India!
    Posts
    194

    Thanks a lot!

    I am grateful to you!

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