I'm trying to access the position of the # symbol and using the following JS...
<script>
$(document).ready(function () {
$("#submitButton").click(function (e) {
var at = $('#email').val().lastIndexOf("#");
});
});
</script>
However this produces a Parse Error:
"");" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid.
Do I need an escape character here? When I replace the # with some other character, say a letter. It does not crash. This code works...
$("#submitButton").click(function (e) {
var at = $('#email').val().lastIndexOf("w");
e.preventDefault();
Assuming you are trying to execute the exact snippet above, the error is telling you that your code is not valid. Specifically, you are missing a closing curly brace (}) and a closing parenthesis ())
Here is what it should look like:
$("#submitButton").click(function(e) {
var at = $('#email').val().lastIndexOf("#");
console.log('at', at);
e.preventDefault();
}); // <-- Closing } and )
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="submitButton">Click Me</button>
<input id="email" type="text" value="something#something.com" />
As far as the # symbol goes, you do not need to escape it in this scenario.
EDIT
The error your getting smells a lot like you're using this in an ASP.NET controller. You might just be including your JS wrong. Try including it by doing it this way:
#scripts {
<script>
$(document).ready(function () {
$("#submitButton").click(function (e) {
var at = $('#email').val().lastIndexOf("#");
});
});
</script>
}
I found it! The # symbol needs an escape character...another # symbol. This works...
var at = $('#email').val().lastIndexOf("##");
Related
I am trying to match block of code to check if is html or javascript code, i tried doing this but am having problem while when have this html element "<div></div>" or php <? echo '';> inside javascript code it will match it as html element.
Please can someone help me with best way to archive this?
<script>
$(document).ready(function(){
function AssignLang(theLanguage){
var regex = /(<([^>]+)>|<([^>]+)>)/ig;
//var regex = "<(\"[^\"]*\"|'[^']*'|[^'\">])*>";
if(theLanguage.match(regex)){
var lang = 'markup';
}else{
var lang = 'javascript';
}
return lang;
}
$('pre code').each(function () {
var the = $(this).html();
/*I tried here to match from 0 to 50 but is not going to help because if the javascript tag begin with <script> still show as html
var theLanguage = the.replace(/\s/g, '').substring(0,50);
*/
var theLanguage = the.replace(/\s/g, '');
var langType = AssignLang(theLanguage);
$(this).addClass("fullcoded language-"+langType);
alert(theLanguage+"-"+langType);
});
});
</script>
Here am matching code inside pre and code element
<pre><code>
function check() {
var delvar = "<? $_POST["del"]; ?>";
var answer = confirm("Are you sure you want to delete the article?")
if (answer) {
window.location = "adm-site.php?del=" + delvar + "&delete=true";
}
}
</code></pre>
<pre><code>
<select name="del">
<option value="none" SELECTED></option>all articles echo'ed here by php
</select>
</code></pre>
Here is a link to https://jsfiddle.net/ppu9qw3n/
This is the regex I'm using /\w+\s\w+\(\)/i
\w+ matches 1 or more a-z, A-Z, 0-9 and _
\s matches 1 space
\( and \) matches ()
Basically I'm searching for a pattern consisting of function functionName() in the code between code tags. If a match is found then it's JavasScript code or else it's HTML code.
This is how you can implement the JavaScript:
$(document).ready(function(){
var regex = /\w+\s\w+\(\)/i;
$('pre code').each(function () {
var v = $(this).html();
if(regex.test(v)){
alert(v+'-'+'THIS IS JAVASCRIPT CODE')
}
else{
alert(v+'-'+'THIS IS HTML CODE')
}
});
});
Check it here on jsfiddle. It's working.
https://jsfiddle.net/swzaf5r1/
I want to add a jQuery code to a PHP string
Here is the PHP code
$my_code ='
<script src="js/jquery.form.js"></script>
<script>
$(document).ready(function()
{
$("#my_form").on("submit", function(e)
{
e.preventDefault();
$("#myButton").attr("disabled", "");
$("#my-output").html(‘<div class="alert alert-info" role="alert">My message</div>’);
$(this).ajaxSubmit({
target: "# my-output ",
success: afterSuccess
});
});
});
function afterSuccess()
{
$("#myButton").removeAttr("disabled");
}
</script>
‘;
Issue I’m having in this code line
$("#my-output").html(‘<div class="alert alert-info" role="alert">My message</div>’);
I even try change the single quote to double quotes and when I do code stopped working. It works fine if I don’t have a div inside above code line.
Ex:
$("#my-output").html(“My message”);
Appreciate your answers.
You can use single quotes inside the string, however you will need to \' escape them:
'$("#my-output").html(\'<div class="alert alert-info" role="alert">My message</div>\');'
You should use Heredoc Syntax to avoid quote issues.
On my view I have this:
#if(ViewBag.Test == true)
{
<script>
window.alert("test")
</script>
}
Here is a picture:
As you can see, the red wave-line is saying that it is an Unterminated string constant.
How do I fix this?
It may be choking on the closing script tag.
Are you able to do something like the following:
string script = "<scr" + "ipt>window.alert('test');</scr" + "ipt>";
Response.Write(script);
I am not able to replace multiple $ signs using JavaScript/jQuery ,
my JavaScript replace code are as per bellow,
var str = $('#amt').html().replace("/\$/g","₹");
alert(str);
but it does not replace all occurrence, Please help me to replace $ by ₹ symbol.
Your regex is correct, but when wrapped it in quotes, it is no longer a RegEx, it's a string.
.replace(/\$/g, "₹");
And the HTML is not replaced it is just creating a string variable, use
$('#amt').html(function (i, oldHtml) {
return oldHtml.replace(/\$/g, "₹");
});
$('#amt').html(function(i, oldHtml) {
return oldHtml.replace(/\$/g, "₹");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="amt">
<div>Books: $150.00</div>
<div>Food: $2050.00</div>
<div>Total: $2200.00</div>
</div>
I keep getting "Unexpected token <" because of this script:
<script type='text/javascript'>
$(document).ready(function(){
if (window.location.pathname + window.location.search = '/search/?q=label:Web-Design|label:Graphic-Design|label:Identity-Design|label:Brand-Design') {
document.write (<style type="text/css">#HTML25, #HTML23, #HTML22, #HTML24 { display:block; }</style>);
}
});
</script>
I don't understand why it's throwing that error. I have been researching for about 2 hours now. I tried adding CDATA tags, I tried using entity names instead of characters, I made sure there was no whitespace within the document.write, etc, etc. Why won't it work? I thought document.write supported HTML entities?
EDIT: I changed the = operator to == . I also added single quotes, but then when I submitted to Blogger I got the XML error: "The content of elements must consist of well-formed character data or markup" so I changed the HTML Characters to HTML Names and resubmitted. I am still getting the "unexpected token" < error...
UPDATE I have updated the script to this, but still get the exact same error:
<script type='text/javascript'>
<![CDATA[
$(document).ready(function(){
if ((window.location.pathname + window.location.search) === '/search/?q=label:Web-Design|label:Graphic-Design|label:Identity-Design|label:Brand-Design') {
document.write ('<style type="text/css">#HTML25, #HTML23, #HTML22, #HTML24 { display:block; }</style>');
}
});
]]>
</script>
At least you have to add a single quote around your string ...
<script type='text/javascript'>
$(document).ready(function () {
if ((window.location.pathname + window.location.search) === '/search/?q=label:Web-Design|label:Graphic-Design|label:Identity-Design|label:Brand-Design') {
// add the style to your head
$('head').append(String.fromCharCode(60) + 'style type="text/css">#HTML25, #HTML23, #HTML22, #HTML24 { display:block; }' + String.fromCharCode(60) + '/style>');
// or decide to individually show the divs with jquery selectors
$('div#HTML25').css('display', 'block');
}
});
</script>
Try this:
<script type='text/javascript'>
$(document).ready(function(){
if (window.location.pathname + window.location.search == '/search/?q=label:Web-Design|label:Graphic-Design|label:Identity-Design|label:Brand-Design') {
document.write("<style type='text/css'>#HTML25, #HTML23, #HTML22, #HTML24 { display:block; }</style>");
}
});