You are viewing limited content. For full access, please sign in.

Question

Question

Javascript Help: field value as variable

asked on November 24, 2015

Hello,

I have an issue, the user fills in the forms & various fields are filled, some are text & others are drop down, after this a text has to be populated in another field, I am having problem in concatenating the text using values of fields, I have tried to make variables out of the fields whose value will be filled, however I am not sure if I am using the correct syntax, can anyone PLEASE help?

var vAufwand = function (){
  
  var AFill = $('#q164 input').val(); // Field type Text
  var AChf = $('#q140 input').val();  // Field type Text
  var TValue = $('#q270 input').val(); // Field type Text
  var PChf = $('#q227 select').val(); // Field type Dropdown
  var PLchf = $('#q228 select').val(); // Field type Dropdown
  var SChf = $('#q229 select').val(); // Field type Dropdown
  var AChf = $('#q230 select').val(); // Field type Dropdown

  if($('#q135 select').val() == 'Pauschalpreis'){
			$('#q272 input').val('Unsere Aufwendungen können wir aufgrund der heute vorliegenden Informationen' + AFill + ' ' + AChf + ' offerieren. Der Preis versteht sich zzgl.' + ' ' + TValue + ' Allfällige Zusatzleistungen würden zusätzlich nach Aufwand mit den üblichen Stundensätzen verrechnet.');
  	  }
   else if ($('#q135 select').val() == 'Richtpreis' | 'Kostendach'){ // I am not sure if this is the correct syntax, but the scenario is , if any of these values is selected the below text should populate
   			$('#q272 input').val('Unsere Aufwendungen können wir aufgrund der heute vorliegenden Informationen'  + AFill + ' ' + AChf + 'offerieren. Die Verrechnung erfolgt zu den Stundensätzen von CHF' + PChf + ' für Partner, CHF' + PLchf + ' für Projektleiter, CHF ' + SChf + ' für Sachbearbeiter und CHF ' + AChf +' für Administration. Die Preise verstehen sich zzgl. '+ TValue + ' Allfällige Zusatzleistungen würden zusätzlich nach Aufwand mit den üblichen Stundensätzen verrechnet.');
	  }
	else if $('#q135 select').val() == 'Nach Aufwand'){
			$('#q272 input').val('Unsere Aufwendungen können wir aufgrund der heute vorliegenden Informationen'  + AFill + ' ' + AChf + 'offerieren. Die Verrechnung erfolgt zu den Stundensätzen von CHF' + PChf + ' für Partner, CHF' + PLchf + ' für Projektleiter, CHF ' + SChf + ' für Sachbearbeiter und CHF ' + AChf +' für Administration.');
	}
  }
  $('#q135').on('blur change', vAufwand);

 

0 0

Answer

SELECTED ANSWER
replied on November 24, 2015 Show version history

There is one little problem I see in your if statement that may cause some issues. In the first "else if" you have:

   else if ($('#q135 select').val() == 'Richtpreis' | 'Kostendach'){ 

and it should be:

else if ($('#q135 select').val() == 'Richtpreis' || $('#q135 select').val() == 'Kostendach') {

You are also missing a parenthesis around the last "else if"

This:

else if $('#q135 select').val() == 'Nach Aufwand'){

should be:

else if ($('#q135 select').val() == 'Nach Aufwand'){

You also may want to use a switch statement rather than an if. It would be a little easier to read.

1 0

Replies

replied on November 24, 2015

Hi Sahil,

Rather than using a variable to store the function it may be best to just declare the function, then call it in the same way you have done above. Also you may need to add 'input' after the field you're attaching the event to (#q135) like this:

function vAufwand() {
//script here
}

$('#q135 input').on('blur change', vAufwand);

Also to use the value of the drop downs you'll have to make sure you've associated values with the choices in the form editor, as shown below:

Hope this helps!

Dan

0 0
replied on November 24, 2015

Thanks Dan,

My whole script works, only the variables not, if I take the variables out of the script rest of the text populates correctly, but in the syntax posted above till the time I have variables inside, it doesn't??

0 0
replied on November 24, 2015

Thanks but that doesn't solve my problem of the variables???

0 0
replied on November 24, 2015

Hmm that's strange, I can't really see any problems with it other than what Chris and I have pointed out frown that is how you create a variable from a text input. Have you tried using the function method rather than trying to call the variable? Maybe it's not possible to declare a variable inside a variable if that makes sense?

replied on November 24, 2015

Hmm that's strange, I can't really see any problems with it other than what Chris and I have pointed out frown that is how you create a variable from a text input. Have you tried using the function method rather than trying to call the variable? Maybe it's not possible to declare a variable inside a variable if that makes sense?

0 0
replied on November 24, 2015

The only thing I would suggest to debug is do a "console.log(variable);" and open dev tools (if in chrome) and see what the output is.

0 0
replied on November 24, 2015

I'll try that, in the meantime can you guide me to use switch? 

I've never used it....

0 0
replied on November 25, 2015

Thanks Chris, it worked as desired!!!

 

I will try to make Switch statement also, just to try...

 

Thanks again!

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.