Jump to content


[help/JavaScript]onChange


4 replies to this topic

#1 joe

    Young Padawan

  • Members
  • Pip
  • 115 posts
  • Location:stuck in the middle of cyber space

Posted 23 February 2008 - 11:12 PM

Hello All... :biggrin:
I've got some problem on JavaScript in here...
This is my script :

<HTML>
 <HEAD>
  <TITLE>---</TITLE>
   <script TYPE="text/javascript">
   function days(x)
   {
	 var number = new Array("31","29","30");
	 temp2 = number[x];

	 document.write("<SELECT>");
	 for(i=1;i<=temp2;i++)
	 {				
	   document.write("<OPTION VALUE=" + i + ">" + i + "</OPTION>");	  
	 }
	 document.write("</SELECT>");
   }
  </SCRIPT>
  </HEAD>
 <BODY>
  <FORM>
   <SELECT NAME="months" onChange="days(months.options[months.selectedIndex].value)">
	<OPTION VALUE="default" SELECTED>--Month--</OPTION>
	<script TYPE="text/javascript">
	 var month = new Array("January","February","March");
	
	 for(i=0;i<3;i++)
	 {
	   document.write("<OPTION VALUE=" + i + ">" + month[i] + "</OPTION>");
	 }
	</SCRIPT>
   </SELECT>
  </FORM>
 </BODY>
</HTML>

My Algorithm is :
1. When I select some a month, and will display a selection day for month based on the month I select in month selection. For example, If I select January and the days from 1 to 31, if i select February and the days from 1 to 29/28, etc.
2. But position display of days selection is beside of month selection. In this script, when i pick a month, the month selection stricken by days selection... :( can somebody help me ??

Thank you very much... :mellow:

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 24 February 2008 - 12:10 AM

Mmk, i understand what you're trying to accomplish (1), i just don't understand what's wrong (2).

I'm assuming writing a <br> in between the selects would fix your problem, but i don't even know if what i'm thinking is the right problem.

I do know what some browsers do not take kindly to calling document.write() after everything has finished loading, so might i suggest using innerHTML instead?
document.forms[0].innerHTML += '<select>';


#3 joe

    Young Padawan

  • Members
  • Pip
  • 115 posts
  • Location:stuck in the middle of cyber space

Posted 24 February 2008 - 12:56 AM

View Postrc69, on Feb 24 2008, 12:10 PM, said:

Mmk, i understand what you're trying to accomplish (1), i just don't understand what's wrong (2).

I'm assuming writing a <br> in between the selects would fix your problem, but i don't even know if what i'm thinking is the right problem.

I do know what some browsers do not take kindly to calling document.write() after everything has finished loading, so might i suggest using innerHTML instead?
document.forms[0].innerHTML += '<select>';

thank you for your reply.... :biggrin:
my problem is : when i select a month from that selection, the month selection disappear and browser only show me the day selection... :( :(
i'm try to : when i select a month in month selection, the month selection didn't disappear and the days selection position is right beside the month selection..
i hope i don't make you confuse... :D :D

thank you very much... :mellow:

#4 communiti.ch

    Young Padawan

  • Members
  • Pip
  • 52 posts

Posted 24 February 2008 - 07:39 AM

try this:
<HTML>
<HEAD>
  <TITLE>---</TITLE>
   <script TYPE="text/javascript">
var tmp = '';
   function days(x)
   {
	 if(x == 'default') { document.getElementById('id_days').innerHTML = 'Please select a month'; }
else{
	 var number = new Array ("31","29","30");
	 temp2 = number[x];

	 tmp = "<SELECT>";
	 for(i=1;i<=temp2;i++)
	 {				
	   tmp = tmp + "<OPTION VALUE=" + i + ">" + i + "</OPTION>";	  
	 }
	 tmp = tmp + "</SELECT>";
	  document.getElementById('id_days').innerHTML = tmp;
   }
}
  </SCRIPT>
  </HEAD>
<BODY>
  <FORM>
<table><tr><td>
   <SELECT NAME="months" onChange="days(months.options[months.selectedIndex].value)">
	<OPTION VALUE="default" SELECTED>--Month--</OPTION>
	<script TYPE="text/javascript">
	 var month = new Array("January","February","March");
	
	 for(i=0;i<3;i++)
	 {
	   document.write("<OPTION VALUE=" + i + ">" + month[i] + "</OPTION>");
	 }
	</SCRIPT>
   </SELECT>
</td>
<td id="id_days">Please select a month
</td></tr></table>
  </FORM>
</BODY>
</HTML>

I have tested it and it works :biggrin:

#5 joe

    Young Padawan

  • Members
  • Pip
  • 115 posts
  • Location:stuck in the middle of cyber space

Posted 24 February 2008 - 07:45 PM

^^^^
thank you so much, for your help.... :) :P
thats' what i'm try to accomplish...

Thank you very much... :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users