Page 1 of 1

javascript

Posted: Wed Mar 29, 2017 9:38 am
by sabinsunny
Hi Team, Today i need a java script

i have a dropdown list and and a textbox;
i need to display the value in textbox which i select in the dropdown list.

Re: javascript

Posted: Wed Mar 29, 2017 5:54 pm
by amosbatto
Do you want the value of the selected option in the dropdown or its label?

To copy the value of the selected option, do this:
Code: Select all
$("#mydropdown").setOnchange( function (newVal, oldVal) {
    $("#mytextbox").setValue(newVal);
});
If you need the label of the selected option, then do this:
Code: Select all
$("#mydropdown").setOnchange( function (newVal, oldVal) {
    var aOptions = $("#mydropdown").getInfo().options;
    for (var i in aOptions) {
        if (aOptions[i].value == newVal) {
            $("#mytextbox").setValue(aOptions[i].label);
            break;
        } 
    }        
});
Where "mydropdown" is the ID of your dropdown and "mytextbox" is the ID of your textbox.
Import this DynaForm for an example:
(2.31 KiB) Downloaded 688 times

Re: javascript

Posted: Thu Mar 30, 2017 3:07 am
by sabinsunny
Hi amosbatto ,
Thank You So Much,

Here i need another coding also

need to take substring from one dropdown box value to another textbox

Re: javascript

Posted: Thu Mar 30, 2017 5:04 am
by mishika
Hello,

To add a substring to another text box, you can make a little modification to the code given by Amos.
Code can be as follows:
Code: Select all
$("#mydropdown").setOnchange( function (newVal, oldVal) {
  var aOptions = $("#mydropdown").getInfo().options;
  for (var i in aOptions) {
    if (aOptions[i].value == newVal) {
      $("#mytextbox").setValue(aOptions[i].label);
      var lab = aOptions[i].label;	//take the label
      var res = lab.split("-");		//split and get an array
      $("#mytextbox1").setValue(res[1]); 	//assign a value from array to a textbox
      break;
    }
  } 
});
You can use .split function to break the string on the basis of a connector like -, ., _ etc.
You can also use .substring function where you will have to pass the starting index and the ending index of the string to be selected from the main string.

Hope this helps

Best Regards
Mishika