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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onChange Function... 2

Status
Not open for further replies.

dwessell

Programmer
Joined
Jan 26, 2006
Messages
114
Location
US
Hey guys/gals...

I'm making my first foray into Javascript, and would appreciate any help you can provide with this function..

<SCRIPT language="JavaScript">
function howMany() {
for(i=0;i<submit.numberTeams.selectedIndex;i++){
document.write ("<input type="text" name="phoneNumbers$i" size="15" maxlength="10"><br>");
}
}
</SCRIPT>

And it's called from:

<select name="numberTeams" onChange="howMany();">
<option> 1 </option>
<option> 2 </option>
</select>


The idea being writing out a new input aspect, based on the input in numberTeams.. Any thoughts would be appreciated.

Thanks
David
 
I don't know if this is the complete solution, but I think this line:
Code:
for(i=0;i<submit.numberTeams.selectedIndex;i++){
should be:
Code:
for(i=0;i<[b]yourformname[/b].numberTeams.selectedIndex;i++){

Hope this helps.
Doc
 
David,

What is wrong with the code? You don't say. What does it do that it shouldn't? What doesn't it do that it should? What errors (if any) do you get? What does your form and form element structure look like?

You'll need to give a bit more to go on!

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hey Dan..

That was kind of a half a**ed post, wasn't it? I guess I should go to sleep sooner.. :)

What it does, is nothing :) The onChange is either ignored, or the function is not executing properly.. IE gies an error that at ')' is expected on the funcitn line:

document.write ("<input type="text" name="phoneNumbers$i" size="15" maxlength="10"><br>");

....

I know of one issue with this line.. JS doesn't represent it's variablas as $i... So I guess the first question would be how can I concatenate phonenumbers + i ?

I'm still not sure of the missing ')' as I see nothing unclosed, but then again, this is my first foray into JS.

Thanks
David
 
I think this is the problem with the ) that you are seeing being reported in IE...
Code:
document.write ("<input type="text" name="phoneNumbers$i" size="15"  maxlength="10"><br>");
Notice your use of quotes (") in the document.write... if you re-write it then you can see the problem...
Code:
document.write ('<input type="text" name="phoneNumbers' + $i + '" size="15"  maxlength="10"><br>');
Javascript uses the + symbol as a concatenator if any one of the things being concatenated is a string.

Make those changes... roll it all together... and if you are still having a problem, post your javascript (with the new changes you have made) and we'll keep playing with it.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Thanks Jeff. You guys are great.. Places like this make learning a new language alot easier...

That did it mostly, I took the $ away from the i, and it's begining to work..

What I had intended, was that depending upon the users selection in numberTeams area, a new form option would be created within the same page.

What it 'appears' to be doing is reloading the same page with the new form, but doing away with all old content.. I suppose reloading isn't the proper term, as it all appears to be client side..

I'm reading some articles on JS, and hopefully this will shed some light on the flaws in my thinking.. Thanks for everyones assistance.

Thanks
David
 
If you use "document.write" after the page has finished loading, then it will overwrite all existing content on the page.

That sounds like what you're seeing.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Is it possible to do what I'm wanting? Or am I barking up the wrong tree?

Thanks
David
 
Take a look at document.getElementById() with a view to setting the HTML of, say, a DIV (with an ID) by setting innerHTML (which is supported by all modern browsers).

Let us know how you go on this.

Apologies about the $i thing - I was still thinking PHP when I was editing it [smile]

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Hey Jeff.. Thanks for the help.. But after reading for quite some time, I'm still lost on how to do it... I think mabye I'll got with onBlur or something a little easier.. Mabye even do it in a seperate page... Thanks for all the help.. I'll be back, once I get some more JS experience, I promise.

Thanks
David
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top