Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pass 2d array to a function from html

Status
Not open for further replies.

justride

Programmer
Jan 9, 2004
251
US
Hello,

I have mutliple HTML
Code:
<select name="requirements[0][]" multiple size=10>
select boxes on my page that get used by PHP. [0][0] - [n]n]

my question is, how can I send this array to a javacript function to verify a value exists at [0][0] in such a way as i do with a text field.

Code:
onsubmit="return IsFormValid('form','unitnumber','unitname',requirements[][]);"
....
function IsFormValid(form,unitnumber,unitnam,requirements){
	var req = document.forms[FormName].elements[requirements[0][0]];
	alert("req:".req.value)
does not work and causes an error in ie.

any help would be awesome

thanks
 
It's the "[" and "]" in the name that causes the problem... If you give it an ID as well, and refer to it using getElementById, that would do the trick.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
<select name="requirements[0][]" id="reqs" multiple size=10>

var reqs = new Array();
reqs = document.getElementById("reqs");
alert("req:".reqs)

this gives an alert with undefined.

alert("req:".reqs[0][0].value) <--- that fails


any suggestions?
 
This
Code:
alert("req:".reqs[0][0].value)

makes no sense at all. There is no string object property array named reqs, which is what you're asking your code to access.

And you're missing some quotation marks in your form element associative array
Code:
var req = document.forms[FormName].elements[[b][red]'[/red][/b]requirements[0][0][b][red]'[/red][/b]];

alert([b][red]'req: ' + req.value[/red][/b]);

You'll also have to actually SPECIFY the FormName or put it in quotes, since you're not passing the name to the function as an argument.

You should really try to understand the code you're working with rather than copy and paste someone else's work and then whack at it like that. It's not that much different from PHP, so it shouldn't be too difficult for you to figure out.

Lee
 
Woah, boy.

You ask one question, I answer it, and then you say something else totally unrelated, and really erroneously put together doesn't work.

Take it slowly, think about what you're asking, and the code you have.

Perhaps even run it in Firefox, and look at the errors in the JS console. You should be able to figure it out from there.

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Sorry for my ignorance here. I went off on a tangent with the getElementByID (researchin online) attribute and lost track of my initial problem. My lack of knowledge combined with IE's wonderful debug mechanisms (j/k) doesnt make learning JS any easier, hence why I resort to alert()'s, unsuccessfully, and this board.

I appreciate the both of your patience and input. I have this worked out now.

Thanks guys.
 
The Firefox browser has a built-in Javascript console that will give you the errors by line and character. I recommend it highly if you're going to do much work with Javascript.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top