Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-02-06
5
Medium Priority
?
271 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 1000 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 1000 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
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 …

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