How to pass global JS var within regexp [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do you pass a variable to a Regular Expression JavaScript?
This is my regexp
regexp:{
spamcheck : /^[15]+$/
}
I need to do this
var spamcheck_sum = 15;
regexp:{
spamcheck : /^[spamcheck_sum]+$/
}
and
Side note: I cant use my spamcheck_sum var outside the js reason is my js file loads first and my var for sum loads after , any way to globalize that var to be acceptable no matter if js file loads first?
working with cms here and js file placement is automatic if I use the cms js placements calls . Joomla

var spamcheck_sum = 15;
regexp:{
spamcheck : new RegExp("^["+spamcheck_sum+"]+$")
}

Related

How to add a variable into a jQuery addClass function [duplicate]

This question already has answers here:
jQuery selectors with variables
(5 answers)
Closed 2 years ago.
I am very new to jQuery but learning. I am trying to do something simple (I think) but struggling. I am trying to add a var into Jquery to customise an specific element.
I need to add 'styler' class to my div which has the id="spinner7" like -
$('#spinner7').addClass('styler');
However the number can vary so I need to add it to the end of #spinner. I have tried things like below but no joy. Can anyone help please.
var ctrl = '7';
$('#spinner +ctrl').addClass('styler');
Template literals
var ctrl = '7';
$(`#spinner${ctrl}`).addClass('styler');
Or
var ctrl = '7';
$('#spinner' + ctrl).addClass('styler');

How to retrieve setAttribute from servlet into JavaScript [duplicate]

This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 6 years ago.
I'm trying to retrieve the value of my setAttribute into my javascript code but I get a syntax error when I run the page.
Here is the code of my setAttribute in my servlet:
request.setAttribute("codeIGA", codeIGA); //a string is being passed here
Here is the code of my JS:
$(document).ready(
function () {
var count = 0;
var lastCodeIGA = ${sessionScope.codeIGA}; // syntax error here
console.log(lastCodeIGA);
.
.
.
etc
How can I retrieve the value from my servlet and input it into my JS?
You can use following to retrieve the required value.
var lastCodeIGA = '${codeIGA}';
Try with scriptlet.
var test = <%=request.getAttribute("codeIGA");
Or
var lastCodeIGA = '${codeIGA}';

Creating JSON without the use of any external library? [duplicate]

This question already has answers here:
Encoding Javascript Object to Json string
(2 answers)
Closed 6 years ago.
SORRY! EDIT ON OLD BROWSERS prior to ie8!
In order to create XML on the fly in JS, one can do the below,
Is there any way I can achieve the same - creating JSON on the fly in JS without the use of any external library?
var parent = document.createElement("parent");
var children = document.createElement('children');
var child1 = document.createElement('child1');
var child2 = document.createElement('child2');
var textNode1 = document.createTextNode("some text1");
var textNode2 = document.createTextNode("some text2");
child1.appendChild(textNode1 );
child2.appendChild(textNode2 );
children.appendChild(child1);
children.appendChild(child2);
parent.appendChild(children);
alert(parent.outerHTML);
Just use JSON.stringify()
i.e.
var o={a:12};
JSON.stringify(o);
results in
"{"a":12}"

How to set json value by using rewire module in node.js [duplicate]

This question already has answers here:
How can I add a key/value pair to a JavaScript object?
(26 answers)
Closed 7 years ago.
//filejson.json
{
"body":
{
"name":"abc"
}
}
//mainFile.js
var readJson = require("/filejson.json")
var req=clone(readJson.body);
I want to change the name of JSON value without changing .json file. I need to set name value using rewire module. Can you help me how to set key value pair dynamically. Is it possible to set like this. Please help me Thanks in advance.
var readJson = require("/filejson.json")
readJson.name = "defg";
Require just use the file, no changes will occur to the file.

PHP $_Get and JQuery .load [duplicate]

This question already has answers here:
Encode URL in JavaScript
(22 answers)
Closed 8 years ago.
I want to load another page via Javascript like this
$("#resultarea").load("searchresults.php?searchstring=" + $("#searchbox").val());
in searchresults.php, a single statement
echo $_GET["searchstring"];
what I type in searchbox appears when searchresults.php is loaded except when I add space and another word, no thing appear at all. I heared it can be done by encoding or somewhat, I searched but didn't find a solution.
Try encodeURIComponent:
var encodedValue = encodeURIComponent($("#searchbox").val());
$("#resultarea").load("searchresults.php?searchstring=" + encodedValue);
Source
Demo

Categories

Resources