Web Analytics Made Easy -
StatCounter Help editing simple "Form Script". - CodingForum

Announcement

Collapse
No announcement yet.

Help editing simple "Form Script".

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help editing simple "Form Script".

    First off, Im sorry for the bad title, but I really couldnt think of anything better.

    Anyway, back on topic.
    I learn by reading through javascript made by others, and try to understand how they were thinking, and how it works.

    I found a script that did not work as it should, and got it working, and shrinking it at the same time.

    The script takes what you write in Form "one" and reverse the text into Form "two". (Not actual name of forms)

    I want to remove this reverse effect, but I can not think of how, as I do not really understand whats left of the script.

    Here is the code;

    Code:
    function flip() {
    var result = flipString(document.f.original.value.toLowerCase());
    document.f.flipped.value = result;
    }
    function flipString(aString) {
    var last = aString.length - 1;
    var result = new Array(aString.length)
    for (var i = last; i >= 0; --i) {
    var c = aString.charAt(i)
    var r = flipTable[c]
    result[last - i] = r != undefined ? r : c
    }
    return result.join('')
    }
    var flipTable = {}
    for (i in flipTable) {
    flipTable[flipTable[i]] = i
    }
    What I wish to keep is the effect this var should have (atleast I think it'd work like this);
    var flipTable = {}

    If i'd remake that and instead add...

    var flipTable = {
    a : 'something',
    }


    im quite sure if you'd write "a" in form "one", it'd show up as "something" in form "two".

    This I would like to find a way to keep, while removing the "reflection".

    I hope someone could do this, and explain the code (preferably both) so I feel I learn something from this.

    Just posting the solution would also help as Id be able to compare the two and probably realize what I know cant.


    here is the entire code if someone is intrested...
    http://pastebin.com/d77fd5771


    Help would ofcourse result in my never-ending love (or atleast, weekly-long love), no puppy-killing for a week, and thanked for your helpful post.
    Last edited by AnsNeer; Apr 2, 2009, 03:24 PM.

  • #2
    Well, simply delete the function flip() which is pretty ancient code BTW, and any reference to it or which calls it, of course.

    Then simply:

    document.form2.fieldname.value = document.form1.fieldname value.



    "The government is going to kill the golden goose". - Retail entrepreneur.

    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.

    Comment


    • #3
      Originally posted by Philip M View Post
      Well, simply delete the function flip() which is pretty ancient code BTW, and any reference to it or which calls it, of course.

      Then simply:

      document.form2.fieldname.value = document.form1.fieldname value.



      "The government is going to kill the golden goose". - Retail entrepreneur.
      Ive tried that, but then no text at all appears in form 2, thanks for a quick answer btw, appriciate it.

      Anyway, not to be a leech, but I would appriciate a working code posted.
      it will not be used anywhere, I simply wish to learn, and this is usualy the way I do just that.

      Basic explanation of the code, added inside it using comments, or written here would also be _very_ appriciated.

      And also, Im sorry for being pretty crappy at this
      Last edited by AnsNeer; Apr 2, 2009, 04:17 PM.

      Comment


      • #4
        I hope I understand your desired result, so try this:
        Code:
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
        <html>
        <head>
        <title>Form</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <style type="text/css">
        
        	 body {margin-top: 35px;}	
        	.nText {padding-left: 8px; font-size: 12pt; font-family: tahoma;}     
        
        </style>
        <script type="text/javascript">		
        
        	var keys = []
        	keys['a'] = "something";
        	keys['b'] = "nothing";
        
        	function init(){
        
        		var origField = document.forms[0]['original'];	
        		var keyValue = document.forms[0]['keyValue'];	
        		origField.onkeyup = function()
        			{
        			 for (each in keys)
        				{
        				 if (each == origField.value)
        					{
        					 keyValue.value = keys[origField.value];					 
        					}
        				}
        			}
        	}
        
        	navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);	
        
        </script>
        
        </head>
        	<body>
        		<form action="" method="post">
        		  
        			<label>Search for Key: <input type="text" name="original" size="10" class="nText"></label>
        
        			<br><br>
        
        			<label>Key Value: <input type="text" name="keyValue" size="10" class="nText" readonly></label>
        
        		</form>
        	</body>
        </html>
        Last edited by 12 Pack Mack; Apr 2, 2009, 07:37 PM.

        Comment


        • #5
          Originally posted by 12 Pack Mack View Post
          I hope I understand your desired result, so try this:
          Code:
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
          <html>
          <head>
          <title>Form</title>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
          <style type="text/css">
          
          	 body {margin-top: 35px;}	
          	.nText {padding-left: 8px; font-size: 12pt; font-family: tahoma;}     
          
          </style>
          <script type="text/javascript">		
          
          	var keys = []
          	keys['a'] = "something";
          	keys['b'] = "nothing";
          
          	function init(){
          
          		var origField = document.forms[0]['original'];	
          		var keyValue = document.forms[0]['keyValue'];	
          		origField.onkeyup = function()
          			{
          			 for (each in keys)
          				{
          				 if (each == origField.value)
          					{
          					 keyValue.value = keys[origField.value];					 
          					}
          				}
          			}
          	}
          
          	navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);	
          
          </script>
          
          </head>
          	<body>
          		<form action="" method="post">
          		  
          			<label>Search for Key: <input type="text" name="original" size="10" class="nText"></label>
          
          			<br><br>
          
          			<label>Key Value: <input type="text" name="keyValue" size="10" class="nText" readonly></label>
          
          		</form>
          	</body>
          </html>
          It didnt really do what the original script did, but thanks for trying.

          Comment


          • #6
            Actually... fixing it so the text is copied from one cell to another... while not getting flipped is rather simple.... just edit the flip() function to do:

            Code:
            function flip() {
            	var result = document.f.original.value.toLowerCase(); // get the contents of text box 1 and switch them to lower case if you don't want to lowercase them.... well, I'm sure you can figure that bit out yourself.
            	document.f.flipped.value = result; //assign the result to text box 2
            }
            And you can essentially remove the rest of the javascript.
            The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

            How to ask smart questions?

            Comment


            • #7
              Originally posted by Eldarrion View Post
              Actually... fixing it so the text is copied from one cell to another... while not getting flipped is rather simple.... just edit the flip() function to do:

              Code:
              function flip() {
              	var result = document.f.original.value.toLowerCase(); // get the contents of text box 1 and switch them to lower case if you don't want to lowercase them.... well, I'm sure you can figure that bit out yourself.
              	document.f.flipped.value = result; //assign the result to text box 2
              }
              And you can essentially remove the rest of the javascript.
              Worked perfectly, and I did understand it.

              Thanks a lot!

              Comment

              Working...
              X