SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Amsterdam, The Netherlands
    Posts
    164
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    [SOLVED] onFocus and onBlur work, onChange does not. Why is that?

    Hi,

    I am trying to pass a value from one field to another, real-time. But onChange does not work here, while onFocus and even onBlur do. Why is that, or how else can I pass a value real-time?

    Code:
    <!doctype html>
    <head>
    <script>
    function passText(str)
    {
        document.testForm.secondField.value = str;
    }
    </script>
    </head>
    <body>
        <form name=testForm action="">
            <input type=text name=firstField size=10 value="" onChange="passText(this.value);" />
            <input type=text name=secondField size=10 value="" />
        </form>
    </body>
    </html>
    [Edit: I found it: it is a structural property of onChange that one has to navigate out of the object for it to take effect. See http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx. And onBlur will do the job as well.]

  2. #2
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can also use keyup as well:
    jsFiddle
    Code HTML4Strict:
    <!DOCTYPE html>
    <html>
    	<body>
    		<form name="testForm">
    			<input type="text" name="firstField" value="" />
    			<input type="text" name="secondField" value="" />
    		</form>
    		<script>
    			var frm = document.testForm,
    				passValue = function(fromEl, toEl) {
    					fromEl.onchange = function() {
    						toEl.value = fromEl.value;
    					};
    					fromEl.onkeyup = function() {
    						toEl.value = fromEl.value;
    					};					
    				};
    			passValue(frm.firstField, frm.secondField);
    		</script>
    	</body>
    </html>

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Amsterdam, The Netherlands
    Posts
    164
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I can't get onkeyup to work in IE7 (other versions not checked), but with onfocus for the second field, assuming the value of the first field, the module is working fine now.

    Thanx for the tip, though.


Bookmarks

Posting Permissions

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