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 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2012
    Location
    Bokaro Steel City, India
    Posts
    21
    Thanks
    14
    Thanked 1 Time in 1 Post

    Exclamation 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!


    Edit:
    The issue with the script is that the alert function is working in both cases: when the input field is blank & even when it's not blank.

    I want the alert to work only in blank input field case; and in the other case I want to add the user input to the innerHTML of <output> tag.
    Last edited by TheAliveWinner; 03-05-2014 at 05:23 PM. Reason: to make the post meaningful
    "It will never rain roses: when we want to have more roses, we must plant more roses."
    - George Eliot

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Code:
    <script>
    	var _mySubmit = function () {
    		var text = document.getElementById('_text').value;	
    		if (text == "") {
    			alert('Please enter some text!');
    		} else {
    			document.getElementById('_output').innerHTML = text;
    		}
    	}
    </script>
    Barring you didn't say what's not working or what its supposed to do, there's a fix for you. I assume its always saying you didn't enter text, and you want it to actually check. But you're basically checking what text it has on page load and never updating the value. This now checks the value on submission instead.

    Out of curiosity, why underscores before every id name? And in the future, please actually put details about the purpose of what you're posting and what's not working. Otherwise, I could have just written any script and really, it would answer your post, just not in any way that is useful to you.

  • Users who have thanked Keleth for this post:

    TheAliveWinner (03-05-2014)

  • #3
    New Coder
    Join Date
    Feb 2012
    Location
    Bokaro Steel City, India
    Posts
    21
    Thanks
    14
    Thanked 1 Time in 1 Post
    Dear Friend,

    1. Thanks a lot for the help!

    2. I have named the IDs/functions starting with an underscore to make them distinguishable from the rest of the code. Or, in other words because I am more comfortable that way.

    3. Thanks, I will definitely put details from the next time.

    ---

    Point 2 elaborated: Friend, I am in a bit of dilemma. Actually, I had very good knowledge of Scripting but I have not coded for over a year and I have forgotten almost everything. But now I want to learn everything again. Now, today I was coding and I made a JS program in which I had a function named "click()". To my surprise, the code didn't work. After exploring the books & web for around two hours, I found that there was really no error in the program. finally, I chaged the name of the function to "myClick()" and it worked fine.

    Now, can you please tell me why did this happen? Is it because click() is a built in function in JS just like "submit()" ?

    This is another cause for me now using underscores to name functions.

    ---
    I hope you understand and will reply to me!
    Last edited by TheAliveWinner; 03-05-2014 at 05:34 PM. Reason: grammar

  • #4
    Regular Coder
    Join Date
    Aug 2012
    Posts
    145
    Thanks
    0
    Thanked 27 Times in 27 Posts
    Hi TheAliveWinner,

    i attempted to do a quick research on this, as i believe i come across situations where changing the name of a function would allow it to run as expected. But in your case of the click() it appears that it doesn't work due to "click()" is inbuilt of javascript button object. The following link is something i found where others have asked this question as well:
    html - javascript function name cannot set as click? - Stack Overflow

    hope this helps
    1 Corinthians 15:3-4 / Ephesians 2:8-9 - What or Who are you living for? Jesus is returning very soon

  • Users who have thanked Brandnew for this post:

    TheAliveWinner (03-06-2014)

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    You should avoid giving names or id's to your variables/functions/arguments/forms words which are HTML/JavaScript methods/properties/attributes such as 'name' or 'id' or 'value' or 'test' or 'text' or 'checked' or 'click' or 'href' or 'closed' or 'go' or 'submit' or 'replace' or 'button' or 'radio' or 'parseInt'.
    And of course you may not give a variable a name which is a Javascript keyword or event such as alert, case, char, confirm, false, int, null, onload, return, this, void, window, and so on.
    Also avoid words such as description, author, keyword and generator which may appear in META tags.
    (and *NEVER* use the name "submit", because then your JS code can't call document.formname.submit() if you ever need to.)

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    TheAliveWinner (03-06-2014)

  • #6
    New Coder
    Join Date
    Feb 2012
    Location
    Bokaro Steel City, India
    Posts
    21
    Thanks
    14
    Thanked 1 Time in 1 Post
    Thanks to both of you!

    Really helpful!


  •  

    Posting Permissions

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