11 Replies - 568 Views - Last Post: 06 November 2012 - 02:26 AM

#1 njgmoorman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 02-October 12

Not adding to array with .push()

Posted 05 November 2012 - 10:01 AM

I'm trying to take whatever is entered into an input box and add it to an array, then alert that array. Everything works except adding to the array. The alert pops up but it says undefined.
var smallLet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
var bigLet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
function addOther() {
	var br5 = document.forms["config"].getElementsByTagName('br')[4];
	var othChars = document.createElement("input");
	othChars.type = "text";
	othChars.name = "othCharBox";
	othChars.id = "othCharBox";
	othChars.setAttribute("onkeypress", "javascript:getOther();");
	if (document.getElementById("othChar").checked) {
		br5.parentNode.appendChild(othChars);
		br5.parentNode.insertBefore(othChars, br5);
	}
	else {
		document.parentNode.removeChild(othChars);
	}
}
function getOther() {
	var othBox = document.forms["config"]["othCharBox"].value;
	for (i=0;i<=othBox.length;i++) {
		var othArray = [];
		othArray.push(othBox[i]);
	}
	function alertBox() {
		alert(othArray[0]);
	}
	setTimeout(alertBox, 100);
}



Is This A Good Question/Topic? 0
  • +

Replies To: Not adding to array with .push()

#2 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Not adding to array with .push()

Posted 05 November 2012 - 10:08 AM

Hello there,

Could you perhaps provide the HTML that this Javascript manipulates?
Was This Post Helpful? 0
  • +
  • -

#3 njgmoorman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 02-October 12

Re: Not adding to array with .push()

Posted 05 November 2012 - 10:16 AM

It's included... Sort of. The HTML manipulated by this is dynamically created.
Was This Post Helpful? 0
  • +
  • -

#4 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Not adding to array with .push()

Posted 05 November 2012 - 10:23 AM

The form (called config) and the related HTML is not included. The script you have given cannot be debugged that easily unless you provide your entire source. I am running a few tests on it to see if I can spot the error, but more HTML would make our lives easier.

** EDIT **
Right, after a little prodding I spotted your mistake I think.

You are trying to access a string like it is an array. The following snippet will return undefined, not 'h' like you seem to expect.

var test = 'This is a test';
alert(test[2]);


You will need to split the string up via other means, such as String.split() which you can find information on here.

This post has been edited by Kruithne: 05 November 2012 - 10:23 AM

Was This Post Helpful? 0
  • +
  • -

#5 njgmoorman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 02-October 12

Re: Not adding to array with .push()

Posted 05 November 2012 - 11:19 AM

Can I turn a string into an array?
Was This Post Helpful? 0
  • +
  • -

#6 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Not adding to array with .push()

Posted 05 November 2012 - 11:25 AM

View Postnjgmoorman, on 05 November 2012 - 11:19 AM, said:

Can I turn a string into an array?


Read my response above and take a look at the split function I mentioned.
Was This Post Helpful? 0
  • +
  • -

#7 njgmoorman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 02-October 12

Re: Not adding to array with .push()

Posted 05 November 2012 - 11:32 AM

I made a couple of changes to the code:
var smallLet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
var bigLet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
function addOther() {
	var br5 = document.forms["config"].getElementsByTagName('br')[4];
	var othChars = document.createElement("input");
	othChars.type = "text";
	othChars.name = "othCharBox";
	othChars.id = "othCharBox";
	othChars.setAttribute("onkeypress", "javascript:getOther();");
	if (document.getElementById("othChar").checked) {
		br5.parentNode.appendChild(othChars);
		br5.parentNode.insertBefore(othChars, br5);
	}
	else {
		document.parentNode.removeChild(othChars);
	}
}
function getOther() {
	var n = document.forms["config"]["othCharBox"].value;
	var othBox = n.split("");
	/*for (i=0;i<=othBox.length;i++) {
		var othArray = [];
		othArray.push(othBox[i]);
	}*/
}
/*<input type="text" name="otherchars" onkeypress="javascript:getOther();" />*/
function genPass() {
	
}


Here it the HTML:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<script src="passgen.js">
</script>
<title>Password Generator</title>
<h1>Password Generator</h1>
</head>

<body>
<form name="config">
<input type="checkbox" id="lowercase" value="lowercase" /><label>Lowercase Letters</label><br />
<input type="checkbox" id="capital" value="capital" /><label>Capital Letters</label><br />
<input type="checkbox" id="numbers" value="numbers" /><label>Numbers</label><br />
<input type="checkbox" id="spaces" value="spaces" /><label>Spaces</label><br />
<input type="checkbox" id="othChar" value="othChar" onclick="javascript:addOther();" />
<label>Other Characters</label><br />
<label>Length: </label><input type="number" name="length" /><br />
<input type="button" value="Generate" />
<br /><br />

<label>Password: </label><input type="text" name="pass" /><br />
<input type="reset" value="Reset" />
</form>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#8 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Not adding to array with .push()

Posted 05 November 2012 - 11:36 AM

Now that you have othBox as an array you should be free to do as you wish with them.

One thing I did notice about the bit you commented out, for future reference, the othArray would be overwritten every time the for loop loops, it should be moved outside the for scope like so.

var othArray = [];
	for (i=0;i<=othBox.length;i++)
		othArray.push(othBox[i]);


However, that is not very important as it's commented out now.
Was This Post Helpful? 0
  • +
  • -

#9 njgmoorman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 02-October 12

Re: Not adding to array with .push()

Posted 05 November 2012 - 03:28 PM

So, with the last code I posted, can you tell me why what isn't commented out isn't working?
Was This Post Helpful? 0
  • +
  • -

#10 njgmoorman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 02-October 12

Re: Not adding to array with .push()

Posted 05 November 2012 - 03:35 PM

I tried printing out othBox using the following:

HTML:
<input type="button" value="button" onclick="javascript:printOther();" />



Javascript:
function printOther() {
     document.write(othBox[4]);
}



And nothing happened when I clicked the button(I did enter five characters so there'd be a othBox[4] to print.
Was This Post Helpful? 0
  • +
  • -

#11 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 826
  • Joined: 25-October 08

Re: Not adding to array with .push()

Posted 05 November 2012 - 09:36 PM

View Postnjgmoorman, on 05 November 2012 - 04:35 PM, said:

I tried printing out othBox using the following:

HTML:
<input type="button" value="button" onclick="javascript:printOther();" />



Javascript:
function printOther() {
     document.write(othBox[4]);
}



And nothing happened when I clicked the button(I did enter five characters so there'd be a othBox[4] to print.


In the statement onclick="javascript:printOther():", the javascript: is not needed in modern browsers.

If you get to the printOther() function, it will reload your script to the initial conditions.
You will lose all entries and selections of your program.
It would be better to use an alert() for testing or a <div> element to write to with an .innerHTML command.
Was This Post Helpful? 0
  • +
  • -

#12 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Not adding to array with .push()

Posted 06 November 2012 - 02:26 AM

Hello,

The below snippet should give you a brief idea of how to achieve what you are after. It's a working example of what I showed you how to do in the previous replies. Adapt the methods used in the snippet for your own application as needed.

<script>
	function printCharacter()
	{
		var characterArray = document.getElementById('testBox').value.split('');
		alert(characterArray[5]);
	}
</script>

<input type="text" id="testBox" value="This is a test"/>
<input type="button" onclick="printCharacter()" value="Print the 6th character"/>

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1