Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Weird syntax error when calling a function for onclick event

I have this script on my page:

var diffedTimeScnds = 600;
var diffedTimeMints = 10;

timer = setInterval(function()
    {
        diffedTimeScnds-- ; 
        document.getElementById('counter').innerHTML = diffedTimeScnds + " left for sending token once again";
        if((diffedTimeMints > 10) || (diffedTimeScnds <= 0))
        {
            document.getElementById('counter').innerHTML = "<a href='' style='color:red' onclick="expireCode()">Send again</a>";
            clearInterval(timer)
        }
    }
,1000)

function expireCode(){
    window.location.href = {!! json_encode(route('auth.login.next.token', 'e')) !!}
}

And I get this error:

Uncaught SyntaxError: unexpected token: identifier

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Which is referring to this line:

document.getElementById('counter').innerHTML = "<a href='' style='color:red' onclick="expireCode()">Send again</a>";

I don’t know really what’s wrong with this line that returns syntax error!

So if you know, please help me, I would really appreciate any idea or suggestion…

>Solution :

When you use

innerHTML = "<a href='' style='color:red' onclick="expireCode()">Send again</a>";
                       //your string ends here    ^

Use ' as with href=''

document.getElementById('counter').innerHTML = "<a href='' style='color:red' onclick='expireCode()'>Send again</a>";

Or if you prefer to have " in your html interchange ' and " in your assignment

document.getElementById('counter').innerHTML = '<a href="" style="color:red" onclick="expireCode()">Send again</a>';

But that doesn’t really matter.

The important thing is: The quotations marks enclosing the string value have to be different from the quotation marks that should end up in the result. Or if that isn’t possible, escape your quotations marks with a backslash \"

    innerHTML = "<a href='' style='color:red' onclick=\"expireCode()\">Send again</a>";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading