Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 17th, 2012, 04:49 PM
Registered User
Points: 17, Level: 1
Points: 17, Level: 1 Points: 17, Level: 1 Points: 17, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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 f1delak1; February 18th, 2012 at 10:50 AM.. Reason: i fix the code
Reply With Quote
  #2 (permalink)  
Old February 17th, 2012, 08:55 PM
Registered User
Points: 17, Level: 1
Points: 17, Level: 1 Points: 17, Level: 1 Points: 17, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I fix the code. I add to files with these peaces of codes. sorry, about this mess!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Build an array dynamically yeva40 Javascript How-To 1 May 27th, 2005 03:09 PM
Randomly choose Operator from Array interrupt Javascript How-To 2 April 9th, 2005 07:49 AM
Dynamically creating an array youngj PHP How-To 0 November 8th, 2004 03:42 AM
Passing php array values to javascript array gkrishna Pro PHP 0 November 6th, 2004 03:20 AM



All times are GMT -4. The time now is 03:56 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.