Results 1 to 2 of 2

Thread: How to choose dynamically values from an array

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    2

    How to choose dynamically values from an array

    hi,
    I have this keypressed function:
    Code:
    function keyPressed(event, input) {
        if (event.keyCode == 8) {
            return true;
        }
         var char = event.which ? event.which : event.keyCode;
         char = String.fromCharCode(char);
         var exers = "1234 1234 1234";
    return (exers.charAt(input.value.length) == char);
    }
    This function allow me to press in order the numbers in array (index0). It is works very well. But i want to add an array with more exercises like:
    Code:
    var exerc = new Array();
         exerc[0]= "1234 1234 1234";
         exerc[1] = "5678 5678 5678";
         exerc[2] = "9012 9012 9012";
    Also, i have a dropdown menu that parser options from a xml file:
    Code:
    <form>
    <select style="width:100px" id='courses'>
    </select>
    </form>
    and my xml file looks like:
    Code:
    <courses>
            <course title="exercise 1">
                <lesson>1234 1234 1234</lesson>
            </course>
            <course title="exercise 2">
                <lesson>5678 5678 5678</lesson>
            </course>
            <course title="exercise 3">
                <lesson>9012 9012 9012</lesson>
            </course>
                .
                .
                .
    </courses>
    *I write the same index because i have two input field.
    I see the first choose (depend on dropdown) in first input, and i rewrite the same exercise in the second input.
    So, it's something like an exercise for me and i stack here.
    - I repeat. It is work with only one index very well. The problem is that, when i add more that one index in the array. Any suggestion about my problem?Javascript it is not my strong point

    I try this but it is doesn't work. Baybe it is totally wrong!
    Code:
    function keyPressed(event, input) {
        if (event.keyCode == 8) {
            return true;
        }
         var char = event.which ? event.which : event.keyCode;
         char = String.fromCharCode(char);
         var exerc = new Array();
           exerc[0]= "1234 1234 1234";
           exerc[1] = "5678 5678 5678";
           exerc[2] = "9012 9012 9012";
      for (i=0;i<exerc.length;i++)
    {
           document.getElementById("courses").selectedIndex;
    }
    return (exers.charAt(input.value.length) == char);
    }
    -------------------------------------------------------------------------------------------------------------------------------------------------------
    Because i am not clear with these peaces of codes, i fix it.
    this is my html page test.html:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <script type="text/javascript" src="jquery.js"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    
    <script type="text/javascript">
    $(document).ready(function() {
        var course_data;     
         $.get('exerc.xml', function(data) { 
            course_data = data;               
            var that = $('#courses'); 
            $('course', course_data).each(function() {
                $('<option>').text($(this).attr('title')).appendTo(that);
            });
        }, 'xml'); 
    
        $('#courses').change(function() { 
            var val = $(this).val(); 
            var that = $('#times').empty(); 
                                            
            $('course', course_data).filter(function() { 
                return val == $(this).attr('title'); 
            })
    		.find("lesson").each(function() { 
                $("#lesson").val($(this).text());   
    		});
        });
    });
    </script>
    
    <script type="text/javascript">
    function keyPressed(event, input) {
        if (event.keyCode == 8) {
            // Allow backspace
            return true;
        }
        // Detect character code: event.which on Firefox, event.keyCode on IE
        var char = event.which ? event.which : event.keyCode;
        // Convert to string
        char = String.fromCharCode(char);
    	var exerc = new Array();
         exerc = "1234 1234 1234";
    	
    	// Compare to character in match string and return result
        return (exerc.charAt(input.value.length) == char);
    }
    </script>	
    </head>
    
    <body>
    <form method="post" action="">
    <input type="text" size="90" id="lesson" />
    </form>
    <form  id="form2" name="form2" method="post" action="">
    <input size="90" type="text" class="textarea" onkeypress="return keyPressed(event, this);" />
    </form>
    
    <form name="form1">
           
         <p>exercices 
                  <select style="width:100px" id='courses'>
                  <option selected="selected">choose...</option>
                  </select>
                  </form> 
    </body>
    </html>
    And this is my xml file exerc.xml:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <courses>
            <course title="exercise 1">
                <lesson>1234 1234 1234</lesson>
            </course>
            <course title="exercise 2">
                <lesson>5678 5678 5678</lesson>
            </course>
            <course title="exercise 3">
                <lesson>9012 9012 9012</lesson>
            </course>
    </courses>
    Well, there are two inputs fields above in the first code. In the first input enter the data from the xml file (parser method). This is depend on the choice from dropdown menu. In the second input i use a function (keyPressed) which allow me to enter the same data that depict in the first input, but in order one by one. This is work very well with the first exercise. The problem is that i want to work with more exercises.I add an array with more exercises like this:

    Code:
    var exerc = new Array();
       exerc[0]= "1234 1234 1234";
       exerc[1] = "5678 5678 5678";
       exerc[2] = "9012 9012 9012";
    I change the first function keyPressed, but it is doesn't work. Beybe it is totally wrong:
    Code:
    function keyPressed(event, input) {
        if (event.keyCode == 8) {
        return true;
        }
        var char = event.which ? event.which : event.keyCode;
        char = String.fromCharCode(char);
         var exerc = new Array();
           exerc[0]= "1234 1234 1234";
           exerc[1] = "5678 5678 5678";
           exerc[2] = "9012 9012 9012";
          for (i=0;i<exerc.length;i++){
          document.getElementById("courses").selectedIndex;
          }
         return (exers.charAt(input.value.length) == char);
       }
    Sorry, about this mess of code!
    Last edited by d.i.l.l.1990; Feb 18th, 2012 at 09:54 AM. Reason: i fix the code

  2. #2

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    2

    Re: How to choose dynamically values from an array

    I fix the code. I add to files with these peace of codes. sorry, about this mess!
    Last edited by d.i.l.l.1990; Feb 18th, 2012 at 09:54 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width