admin管理员组文章数量:1335107
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
4 Answers
Reset to default 3you 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.
本文标签: javascriptjquerySyntaxError unterminated string literalStack Overflow
版权声明:本文标题:javascript - jquery:SyntaxError: unterminated string literal - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742383865a2464630.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论