Solved

JS Error - Can't figure out how to write this...

Posted on 2014-02-06
5
269 Views
Last Modified: 2014-02-07
I'm posting a form to a jquery/.js page. I only want to send the value of searchstring if it isn't empty. Otherwise I want to send the other two fields.

var getSearchFormData = function(){
	var frm = $('form#sendemailform'),
		frmData = {
			 // if search string isn't empty search only on that field 
			 	if ($.trim($('#searchString').val()).length > 0){
			 	Name: $('#searchstring').val(),
				status: "active"
				}
				else
				{
			 	InstanceID: $("select[name=sendtoorg]", frm).val().join(","), 
				seclev: $("select[name=sendtorole]", frm).val().join(","),
				status: "active"
				}
	return frmData;
};

Open in new window


I'm getting an error on the first if line.
0
Comment
Question by:traport
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 33

Accepted Solution

by:
Big Monty earned 250 total points
ID: 39839270
i'm not sure how you can do conditionals directly in your json data, but you should be able to do:

var getSearchFormData = function(){
	var frm;
        if ($.trim($('#searchString').val()).length > 0){
            frm = $('form#sendemailform'),
		frmData = {
			 	   Name: $('#searchstring').val(),
			 	   status: "active"
                }
         }
         else {
                frm = $('form#sendemailform'),
			 	InstanceID: $("select[name=sendtoorg]", frm).val().join(","), 
				seclev: $("select[name=sendtorole]", frm).val().join(","),
				status: "active"
		}
         }
	return frmData;
};

Open in new window

0
 
LVL 14

Assisted Solution

by:Pierre Cornelius
Pierre Cornelius earned 250 total points
ID: 39841172
You are declaring a JSON variable but putting code inside it. It expects "Name"/value pairs.

Big Monty's suggestion should work, but needs some corrections:
var getSearchFormData = function()
{
	var ss = $('#searchstring').val();
        if ($.trim(ss).length > 0){
		var frmData = {
		 	   "Name": ss,
		 	   "status": "active"
                }
         }
         else {
		var frm = $('form#sendemailform');
		var frmData = {
		 	"InstanceID": $("select[name=sendtoorg]", frm).val().join(","), 
			"seclev": $("select[name=sendtorole]", frm).val().join(","),
			"status": "active"
			}
         }
	return frmData;
}

Open in new window

0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 39841186
Another way to write this is:

var getSearchFormData = function()
{
	var ss = $('#searchstring').val();
	var frm = $('form#sendemailform');
	return ($.trim(ss).length > 0) ? {"Name": ss, "status": "active"} :{
		 	"InstanceID": $("select[name=sendtoorg]", frm).val().join(","), 
			"seclev": $("select[name=sendtorole]", frm).val().join(","),
			"status": "active"
			};
}

Open in new window

0
 

Author Closing Comment

by:traport
ID: 39841734
Really appreciate it.
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 39843742
Glad to help :)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question