Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery JS Uncaught Error: Syntax error, unrecognized expression:

Tags:

So, I have this issue that has been bugging me and I just can't seem to fix it. The error that comes up is this:

Uncaught Error: Syntax error, unrecognized expression: [{"type":"header","subtype":"h1","label":"Another test form"},{"type":"checkbox-group","label":"Checkbox Group","name":"checkbox-group-1497353080484","values":[{"label":"Option 1","value":"option-1","selected":true}]},{"type":"header","subtype":"h1","label":"Header"},{"type":"number","label":"Number","className":"form-control","name":"number-1497353081884"},{"type":"text","label":"Text Field","className":"form-control","name":"text-1497353083345","subtype":"text"}]

I have managed to track down the line of code that is causing this issue, which is this:

        forms = $(document.getElementById('formData').getAttribute("value")),

Now, I have no idea why this is causing this issue. The content coming in is correct. This is all being done within a JQuery function, in fact the whole JQuery function looks like this:

jQuery(function($) {
		var $fbEditor = $(document.getElementById('fb-editor')),
		$formContainer = $(document.getElementById('fb-rendered-form')),
		forms = $(document.getElementById('formData').getAttribute("value")),
		fbOptions = {
			formData: forms,
			dataType: 'json',
			onSave: function(){
				$fbEditor.toggle();
				$formContainer.toggle();
				$('form', $formContainer).formRender({
					formData: formBuilder.formData
				});
			}
		},
		formBuilder = $fbEditor.formBuilder(fbOptions);
		
		$('.edit-form', $formContainer).click(function() {
			$fbEditor.toggle();
			$formContainer.toggle();
		});
	});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

So, I am not that experienced with JavaScript, JQuery or any of the rest of this. So it could very well be a simple issue but I cannot currently figure out what it is. Can somebody else tell me where I have gone wrong and possibly how to fix it?

like image 445
Michael Ingram Avatar asked Jun 21 '17 12:06

Michael Ingram


2 Answers

forms = $(document.getElementById('formData').getAttribute("value")),

Try removing the $() in the code above. As per poohitan, its a selector but it doesn't appear to be doing anything at all.

like image 174
ugotchi Avatar answered Oct 04 '22 16:10

ugotchi


From the error I guess document.getElementById('formData').getAttribute("value") is a JSON string.

You have wrapped it into jQuery object constructor $(...), and it fails because jQuery expectes the parameter to be a selector (like .classname), DOM element, or another jQuery element.

like image 33
poohitan Avatar answered Oct 04 '22 15:10

poohitan