Solved

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

Posted on 2014-02-06
5
263 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
  • 3
5 Comments
 
LVL 32

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now