最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery:SyntaxError: unterminated string literal - Stack Overflow

programmeradmin0浏览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?

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?

Share Improve this question asked Apr 29, 2014 at 6:44 DanDan 2,17411 gold badges74 silver badges145 bronze badges 4
  • Quote your HTML attribute value properly – Gumbo 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>'; – Tushar Gupta - curioustushar Commented Apr 29, 2014 at 6:52
  • @TusharGupta Yes, your ment worked for me.. Can anyone describe your solution. – Dan Commented Apr 29, 2014 at 6:55
  • StackOverflow should make essential to write ment if -ve vote is done! – Dan Commented Apr 29, 2014 at 17:24
Add a ment  | 

4 Answers 4

Reset to default 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>

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;

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

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.

发布评论

评论列表(0)

  1. 暂无评论