0

when ever i am giving a space between a word & i click to call a function, then i am getting the below error:

SyntaxError: unterminated string literal
[Break On This Error]   

$(this).SelectProjectBox(363,"ssss

mypage# (line 1, col 29)

Browser generated html:

<span ssasas")="" onclick="$(this).SelectProjectBox(363,"ssss" href="#">ssss ssasas</span>

My actual code

  echo '<span href="#"  onClick=$(this).SelectProjectBox(' . $cat->project_id . ',"'.$cat->project_name.'")>' . $cat->project_name . '</span>';

what is the cause Please suggestion, how can i modify my php code for this?

4
  • Quote your HTML attribute value properly Commented Apr 29, 2014 at 6:46
  • @Sundara Try echo '<span href="#" onClick="$(this).SelectProjectBox(\''.$cat->project_id .'\',\''.$cat->project_name.'\')">'.$cat->project_name.'</span>'; Commented Apr 29, 2014 at 6:52
  • @TusharGupta Yes, your comment worked for me.. Can anyone describe your solution. Commented Apr 29, 2014 at 6:55
  • StackOverflow should make essential to write comment if -ve vote is done! Commented Apr 29, 2014 at 17:24

4 Answers 4

3

you missing quotes on onclick event try

echo '<span href="#"  onClick="$(this).SelectProjectBox(' . $cat->project_id . ','.$cat->project_name.')">' . $cat->project_name . '</span>';

or try

<span href="#"  onClick="$(this).SelectProjectBox(<?php echo $cat->project_id ;?>,<?php echo $cat->project_name;?>)"><?php echo $cat->project_name;?></span>
Sign up to request clarification or add additional context in comments.

2 Comments

Quotes appearing in an unquoted value are not allowed.
error: Uncaught SyntaxError: Unexpected token ILLEGAL Html: <span href="#" onclick="$(this).SelectProjectBox(&quot;363&quot;,&quot;ssss" ssasas")="">ssss ssasas</span>
1

If I may give you some advice: split up your code a bit more. It is easier for people to understand:

$content  = "<span href='#' ";  
$content .= "onClick=\"$(this).SelectProjectBox($cat->project_id,'$cat->project_name')\"";
$content .= ">";
$content .= $cat->project_name . "</span>";

echo $content;

3 Comments

if i do so then, my code will be long. already i have 832 lines in my code
But it will be much better for maintenance. And if other programmers have to work with this code it will take them ages to understand. If your file is really getting to big then you could split it up in seperate files.
And as you see, when you made a mistake in your own code it took a long time(and even a SO question) to correct it. Just consider my code. Rather use 4000 lines of readable code then 800 of unreadable code.
1

Escape single quote(') using \.

\' will echo '

To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash, double it (\\). All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning.

Code

echo '<span href="#"  onClick="$(this).SelectProjectBox(\''.$cat->project_id .'\',\''.$cat->project_name.'\')">'.$cat->project_name.'</span>';

Read String

Comments

0

Instead of

echo '<span href="#"  onClick=$(this).SelectProjectBox(' . $cat->project_id . ',"'.$cat->project_name.'")>' . $cat->project_name . '</span>';

try with

echo '<span href="#"  onClick="$(this).SelectProjectBox(\''.$cat->project_id .'\',\''.$cat->project_name.'\')">'.$cat->project_name.'</span>';

Here, we are echoing the html part and we are using single quotes for enclosing the entire html string. So whenever we want single quotes in the string , here we want it for functions, we have to escape it using backslash as /' which will display a single quote. Otherwise the string will get stopped at that point.

3 Comments

SyntaxError: unterminated regular expression literal [Break On This Error] $(this).SelectProjectBox('359','ddddddddd')>ddddddddd</span><img alt= analytics.js (line 16, col 54)
now worked. Can you please describe your answer, so that next time i can solve this type of issue by myself
@Sundra..Please refer this link php.net/manual/en/language.types.string.php This will help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.