Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
html input 8 42
ajax jquery 3 25
HowTo set a global variable in javascript or jQuery 4 21
why JSON Array data not binding on Model ASP.NET ? 1 8
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to dynamically set the form action using jQuery.

809 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