jQuery error = SyntaxError: missing : after property id [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
There seems to be a lot of these errors on SO but I can't find any to relate to.
The error is: SyntaxError: missing : after property id and it's complaining about this line: var msg = ""; (Line 3).
function checkSubmission()({
var msg = "";
$('#validation').html(msg);
if ($("#lms_name").val().length<=0){
msg+="LMS Name Required.<br/>";
}else{
var value = $('#lms_name').val();
var regex = new RegExp(/^[a-zA-Z0-9 ]{2,}$/);
if(value.match(regex)) {
} else {
msg+="<b>Error on LMS name : </b>Numbers and letters only. Minimum of two characters.<br/>";
}
}
//lots more code here...
Any ideas? FYI I'm a complete newbie (for now...).
Thanks in advance!

The code in your comment on #Shoaib Raza, prompted me to post this. (please accept his answer rather than mine, since his was first.)
Quote OP Comment:
FYI, I changed it to just function checkSubmission( var msg = ""; ... and the error still appeared.
And that's totally wrong too. checkSubmission( ... is not what his answer is showing, so the new error message is accurate: SyntaxError: missing ( before { -> function checkSubmission{
This line of your original code contains the original syntax error...
function checkSubmission()({
should be this...
function checkSubmission() {
Notice the difference? You simply need to remove the extra ( and nothing else.

Change your function to :
function checkSubmission()
{
var msg = "";
$('#validation').html(msg);
if ($("#lms_name").val().length<=0)
{
msg+="LMS Name Required.<br/>";
}
else
{
var value = $('#lms_name').val();
var regex = new RegExp(/^[a-zA-Z0-9 ]{2,}$/);
if(value.match(regex))
{
}
else
{
msg+="<b>Error on LMS name : </b>Numbers and letters only. Minimum of two characters.<br/>";
}
}
//lot more code in the function here.. if any
}
Hope this helps.

Related

? Incorrect regex expression [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I’ve been trying to do find if a variable contains a digit inside of it. And for some reason, I put (exp.) ‘Majorpassword09’ and it just prints a false?
// vars
var regex = new RegExp(‘.*\d.*’)
var str = ‘Scorpio08’
// main
function reg1() {
if (regex.test(str)){
console.log(‘true’);
}else{
console.log(‘false’);
}
}
reg1();
EDIT: I’ve put the title as Incorrect regex expression because that's the only thing that I think is wrong.
You're most likely on a mac.
Your code shows that you're using fancy quotes. Like these ones: ‘’. You need to use normal quotes, like ', and ".
Here's a solution to your problem.
// vars
var regex = new RegExp(".*\\d.*")
var str = "Scorpio08"
// main
function reg1() {
if (regex.test(str))
{
console.log("true");
}else{
console.log("false");
}
}
reg1();
Whatever text editor you're using, it has fancy quotes on. Consider looking in the settings, or on Google, to find out how to disable them.
as you are using string to create a RegExp Object thus have to use two backslashes to properly escape
var regex = new RegExp(‘.*\\d.*’)
or
var regex = /.*\d.*/

Javascript won't recognise json element as number [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm processing a json object and want to display a number with two decimal places, but javascript says the type is 'undefined'.
I've tried using .toFixed(2), Number() and ParseFloat() to force it to be a number, and I've even tried String() to force it to change to a string, but it stubbornly remains as 'undefined'. typeof isn't even recognised as a property of the original element.
Surely there's a way to convert it / properly define it. I'd appreciate help and an explanation of why what I'm doing doesn't work.
data = {
list: {
9: {
balance: 256.3999999999942
}
},
action: 'load',
status: 'ACCTLOADED'
}
showAccounts(data);
function showAccounts(data) {
action = data.action;
status = data.status;
accs = data.list;
if ((action == 'load') && (status == 'ACCTLOADED')) { // data retrieved
$.each(accs, function(acc, details) { // display each account
let bal = 0;
bal = Number(details.balance);
console.log('bal ' + bal + bal.typeof);
});
}
} // end function showAccounts
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
I think bal is already type of number.
Use like:
console.log('bal ' + bal + typeof bal);

JavaScript unexpected token syntax error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
What might be causing this error?
My code:
var a = ()
var s = 0
if (a == s) {
console.log('in')
}
The error:
SyntaxError { "Unexpected token" (1:23) }
var a = [];
var s = 0;
if (a == s) {
console.log('in');
}
Why is this being caused? - The characters () right now don't match the syntax of any normal use case of parenthesis. The ways to use parenthesis are here:
define a function: var a = function() { console.log("foo")}
call a function: var a = Math.random(1)
define an arrow function: var a = () => console.log("foo")
wrap any kind of expression: var a = (true && false)
Since in your code's syntax doesn't fit any of those it is giving a Syntax error.
The code above makes the code work using []. [] creates an empty

Function not returning the value [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am working on a javascript quiz programme , and i have return a function to check what difficulty level the user wants . below is the code and the jsfiddle :
function getdifficulty(){
var j = 0;
var level = prompt('what level would you like 1. easy 2. intermediate 3.hard' , '')
if(level == easy){
j = questionseasy[0];
}
else if(level == intermediate){
j = questionseasyenuf[0];
}
else{
j = questionshard[0];
}
alert("you did it");
}
getdifficulty();
Jsfiddle here
now the problem is the the alert is not showing up ? whats the problem with this short piece of code ? (In the real programme though i will not use an alert but return statement , i even tried using document.write or console.log but none worked) .
prompt() returns a string. You need to make your comparisons strings by encapsulating them in double quotes (").
function getdifficulty(){
var level = prompt('what level would you like 1. easy 2. intermediate 3.hard' , '')
if(level == "easy"){
alert("easy");
}
else if(level == "intermediate"){
alert("intermediate");
}
else{
alert("hard");
}
}
getdifficulty();
JSFiddle
Also, in the implementation you have provided in your post, questionseasy, questionseasyenuf, and questionshard will not be defined. You must bring them into the scope of the function before you can start using them.
questionseasy is undefined. You can trace javascript as it runs in your browser console and see this.

appendChild null | javascript/html [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm following a tutorial online on how to make a to do list, found here:
https://www.youtube.com/watch?v=MURDw0-BiEE
I've following it pretty closely, but it won't work. Browser states the appendChild is null. The tutorial is 2 years old, could a part of the code be outdated? I had it sending alerts through the button, but when I changed to the appendChild things stopped working.
I'm pretty new to this and really appreciate the help.
function addNewItem() {
var listItem = document.createElement("li");
listItem.innerText = "Hello";
list.appendChild(listItem);
}
var btnNew = document.getElementById("btnAdd");
btnNew.onclick = function() {
addNewItem(document.getElementById("todoList"));
};
And here's the related part of the HTML:
<p><button id="btnAdd">New Item</button></p>
<ul id="todolist">
</ul>
<script src="todo.js"></script>
</body>
You have defined your function without arguments and then you try to pass one :
function addNewItem(list) {}
P.S. : You also tried to getElementById todoList instead of todolist, so it also gave you error :
addNewItem(document.getElementById("todolist"));
JSFiddle
You've missed function parameter, list:
function addNewItem(list) {
var listItem = document.createElement("li");
listItem.innerText = "Hello";
list.appendChild(listItem);
}
var btnNew = document.getElementById("btnAdd");
btnNew.onclick = function() {
addNewItem(document.getElementById("todoList"));
};
You are passing a parameter to the function addNewItem() with addNewItem(document.getElementById("todoList")), but you aren't declaring this parameter in your function. So the variable list becomes null and appendChild() does not work. So change your first line:
function addNewItem() {
to
function addNewItem(list) {
and it will work.

Categories

Resources