Solved

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

Posted on 2014-02-06
5
270 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

636 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