Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

JavaScript - Textfield Value Gone?

Options
  • 06-06-2007 3:21pm
    #1
    Registered Users Posts: 527 ✭✭✭


    Hi all,

    This is my first attempt at writing in JavaScript, I have previous experience in Java.

    I have written a JavaScript function in a HTML page to perform a basic mathematical operation on some values from Drop-Down lists etc, and once a Button is clicked the total should be displayed in a read-only Textfield on the HTML page.

    The problem I have is that, once the button is clicked the value is passed to the Textfield only for a split second and then blank.

    I am taking a complete stab at this one but is it something to do with me needing to create a cookie for the duration of the session?

    Here's some of the code I done anyway:
    
    // Form Code
    <form id="form1" name="form1" method="post" action="">....
    
    // Button Code
    <input type="submit" name="Submit" value="Calculate Cost" onClick="calc(this.form)"/>
    
    // Textfield Code
    <input name="textfield" readonly="readonly" style="border:1" />
    
    // Inside the <HEAD>
    <SCRIPT LANGUAGE="JavaScript">
    	function calc(form)
    	{
    		...
    		...
    		...
                    // This value 'asd' just disappears?
    		form.textfield.value = "asd"; 	
    	}
    </script>
    
    
    



    Thanks in advance people.


Comments

  • Moderators, Politics Moderators Posts: 39,822 Mod ✭✭✭✭Seth Brundle


    Could the page be submitting to itself and therefore acting like a refresh.
    Change your submit button to a button button.
    // Form Code
    <form id="form1" name="form1" method="post" action="">....
    
    // Button Code
    <input type="[b]button[/b]" name="Submit" value="Calculate Cost" onClick="calc(this.form)"/>
    
    // Textfield Code
    <input name="textfield" readonly="readonly" style="border:1" />
    
    // Inside the <HEAD>
    <SCRIPT LANGUAGE="JavaScript">
    	function calc(form)
    	{
    		...
    		...
    		...
                    // This value 'asd' just disappears?
    		form.textfield.value = "asd"; 	
    	}
    </script>
    


  • Registered Users Posts: 527 ✭✭✭Sean^DCT4


    That's exactly what was wrong with it. Well spotted.

    Thanks a lot kbannon, it was very much appreciated.


    Seany


  • Registered Users Posts: 1,127 ✭✭✭smcelhinney


    Is "this.form" an actual pointer?? News to me.. Anyhow, your problem is that the form is actually submitting, and the values are resetting.

    Better using the proper DOM. Change the code to ..
    // Form Code
    // return false prevents it from submitting the form. 
    <form id="form1" name="form1" method="post" onSubmit="calc(this);return false;">....
    
    // Button Code
    <input type="button" name="Submit" value="Calculate Cost" />
    
    // Textfield Code
    <input name="textfield" readonly="readonly" style="border:1" />
    
    // Inside the <HEAD>
    <SCRIPT LANGUAGE="JavaScript">
    	function calc(form)
    	{
    		...
    		...
    		...
                    // This value 'asd' just disappears?
    		form.textfield.value = "asd"; 	
    	}
    </script>
    

    Let me know if that works.

    EDIT: Or disregard me the slow typist, and do exactly what kbannon says.. :D


Advertisement