JavaScript - Javascript ComboBox Calc2 - Asked By Mabd Adam on 13-Mar-12 10:19 AM

Earn up to 10 extra points for answering this tough question.
I need to add modification relate with:  Button value GOTHERE and input-text name OP,

So I added myself two more inputText boxes (PAID and REST), so i need when i click on GOTHERE button and it output result in inputTex OPalso same number it output in textBox REST, then when i write some Number in PAID textBox, it cuts automatically from REST textBox.

AND HERE IS THE NORMAL CODE:

<form name="dynamiccombo">

<select name="stage2" size="1" onChange="displaysub()">
<option value="#">This is a place Holder text </option>
</select>

<input type="button" name="test" value="Go!" onClick="gothere()"> &nbsp;

Currency: <input name="op"  /><p><br>

Paid: <input class="paid" name="paid" id="paid" onChange="update()"/><p>
Rest: <input class="rest"  name="rest"  id="rest"   onChange="update()" /> <br>

</form>

<script>
<!--
//2-level combo box script- by javascriptkit.com

//STEP 1 of 2: DEFINE the main category links below
//EXTEND array as needed following the laid out structure
//BE sure to preserve the first line, as it's used to display main title

var category=new Array()
category[0]=new Option("SELECT A CATEGORY ", "") //THIS LINE RESERVED TO CONTAIN COMBO TITLE
category[1]=new Option("Shose", "combo1")
category[2]=new Option("Clothe", "combo2")

//STEP 2 of 2: DEFINE the sub category links below
//EXTEND array as needed following the laid out structure
//BE sure to preserve the LAST line, as it's used to display submain title

var combo1=new Array()

combo1[0]=new Option("Sandal","38")
combo1[1]=new Option("Boot","50")
combo1[2]=new Option("BACK TO CATEGORIES","") //THIS LINE RESERVED TO CONTAIN COMBO SUBTITLE

var combo2=new Array()

combo2[0]=new Option("Shirt","30")
combo2[1]=new Option("T-Shirt","45")
combo2[2]=new Option("BACK TO CATEGORIES","") //THIS LINE RESERVED TO CONTAIN COMBO SUBTITLE

var curlevel=1;
var cacheobj=document.dynamiccombo.stage2;

function populate(x){

 for (m=cacheobj.options.length-1;m>0;m--){
  cacheobj.options[m]=null;
 }

 
selectedarray=eval(x);
 for (i=0;i<selectedarray.length;i++){
  cacheobj.options[i]=new Option(selectedarray[i].text,selectedarray[i].value);
 }
 cacheobj.options[0].selected=true
}

function displaysub(){
 if (curlevel==1){
  populate(cacheobj.options[cacheobj.selectedIndex].value);
  curlevel=2;
 }
 else {
  gothere();
 }
}


var MyVariable; // Global Variable

function gothere(){
 if (curlevel==2){
  MyVariable=eval(cacheobj.options[cacheobj.selectedIndex].value);
  if (MyVariable){
   var frm=cacheobj.form;
   frm.op.value=MyVariable  + " US"; 
  }
  else {
   curlevel=1;
   populate(category);
  }
 }
}


//SHOW categories by default

populate(category)
//-->
</script>


Thanks in advance.

D Company replied to Mabd Adam on 19-Mar-12 12:27 PM
Ok, i got your requirement but dont understand why u have called onchange function for rest textbox(i dont think so it needed), here below i'm giving some sample how u can write your onchange function.second thing u can also call the same function on click of GOTHERE button(if needed do bit modification like remove unnecessary calculation)

function onchange()
{
var paid=document.getElementById('txtPaid');//fetch the id of paid textbox
var rest=document.getElementById('txtPaid');//fetch the id of  rest textbox

//now check for null , if the id is getting properly(put alert and see correct the syntax if required)
var restvalue=rest.value;
var paidValue=paid.value;

//now check if there is any value in paid textbox,before that also validate to take only integer not string and other User friendly checks
var remainingamount =restvalue-paidvalue;

//check if it not number or greater then 0
//now u again assign this remaining amount to rest text box.

rest.value=remainingamount ;
//and fetch the id of OP textBox and as per your requirement assign this value to OP also

}

Note: - this javascript code i have just typed like an algorithm check the proper syntax and all.but the basic idea is same
Hope this helps
Mabd Adam replied to D Company on 20-Mar-12 04:06 AM
My Dear,

Thank you for your sincerity answering, and i tell you that i was learning my self for 4 years  only html, css and now i am inside Javascript, only teacher i had was PHD holders Prof. Google and Prof. Youtube thanks both of them.

So my dear after long suffering i solved this toppic as you can see http://aamaguul.net/Js_Combo_Learning.html 

and as you know more than me you can recorrect if there is shortcut way, but no i need to change it to time or hours, Example i pretend selleing Telephone and change Shoes and Clothe to USA and Canada :

the i will say 1 minute of USA  = 10 $, and 1 minute of Malaysia = 15 $

Best Regards