Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
i want to split this string in JavaScript.
i try it:
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to display the array values after the split.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var str = "<new_a><new_b><sb_c>";
var res = str.split("<");
document.getElementById("demo").innerHTML=res;
}
</script>
</body>
</html>
result:
new_a> , new_b> , sb_c>
i want to my result : new_a , new_b , sb_c
how can i do?
function myFunction()
{
var str = "<new_a><new_b><sb_c>";
str = str.replace("<","");
str = str.replace(">"," ");
var res = str.split(" ");
document.getElementById("demo").innerHTML=res;
}
Try this code.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
im trying to do some Math in a js function while using MathJs & getting this error :Math.evaluate is not a function
<script src=" https://cdnjs.com/libraries/mathjs"></script>
Do-Math :<input id="formula" />
<button onclick="goCalc()">Go</button>
results:<input id="resTxt"/>
<script type="text/javascript">
function goCalc() {
let Mformula = $("#formula").val();//the formula im trying to get =6*4+2^2
var res = document.getElementById("resTxt");
// res.setAttribute("value", Mformula);
var mat = Math.evaluate(Mformula);
res.setAttribute(mat);
console.log(mat);
}
It looks like you might be experiencing multiple problems based on the code you've shown. It's not exactly clear which are true issues and which might be related to typos, etc. so here's a complete example demonstrating how to include the library as well as evaluating the text value of an input element each time it changes, writing the result to another element:
body { font-family: sans-serif; } #expression, #result { font-family: monospace; font-size: 1rem; } #expression { padding: 0.5rem; }
<label>Expression: <input id="expression" value="6 * 4 + 2 ^ 2" /></label>
<pre><code id="result"></code></pre>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/11.0.1/math.min.js" integrity="sha512-B82WLflI1EQiR9sGbIV1ddGVvK4ghj1xjMShL7YvcOrHjX2qP72lHztT1DxBVPiz1aTR6mOUJbtwj06uadL2GA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script type="module">
const input = document.getElementById('expression');
const output = document.getElementById('result');
function evaluate () {
const expression = input.value.trim();
let result;
try {
result = math.evaluate(expression);
}
catch (ex) {
if (ex instanceof Error) result = ex.toString();
else throw ex;
}
output.textContent = result;
}
input.addEventListener('input', evaluate);
evaluate();
</script>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I want something similar to f" {variable} " of python
Ive tried
function press()
{
for(var i=0; i<4; i++)
{
if(!document.getElementById('ip'+i).value)
{
alert("error");
break;
}
}
but it didnt work.
I want something similar to f" {variable} " of python
template strings ${variable}
In JavaScript you can use template strings
The usage is
` ${variable} `
document.getElementById('btn').addEventListener('click', press)
function press() {
for (var i = 0; i < 4; i++) {
if (!document.getElementById(`ip${i}`).value) {
alert("error");
break;
}
}
}
<input id="ip0"/>
<input id="ip1"/>
<input id="ip2"/>
<input id="ip3"/>
<button id="btn">Click</button>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need wrap words with   in span, for example:
<p>Word word and word2? But word word to.</p>
Should become:
<p>Word <span>word and word2?</span> But word <span>word to.</span></p>
I wanna do this with regular expression in JavaScript.
You can do something like this https://regex101.com/r/bUflp4/2 for you regex. Be care however, it won't accept tag inside others.
For the replacement, you can use something like this
var elements = document.getElementsByClassName("text-to-replace");
for (var i = 0; i < elements.length; i++) {
var innerHTML = elements.item(i).innerHTML;
var regex = new RegExp('([^ ,<]* [^ ,<\/]*)', 'ig');
var text = innerHTML.replace(regex, '<span class="span-to-add">$1</span>');
elements.item(i).innerHTML = text;
}
.span-to-add {
background-color: yellow;
}
<div class="container">
<p class="text-to-replace">Word word and word2? But word word to.</p>
<p class="text-to-replace"> word this is an example</p>
<p class="text-to-replace">this is another example</p>
</div>
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to get a specific out put based on what is in the text box, with java script but all the code I'm using only works for numbers not with "text". When I replace the numbers with "text" it outputs no matter whats in the text box.
<!DOCTYPE html>
<html>
<body>
<imput id="x"value=""/>
<button on Click="myFunction():">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var qpw = "eighteen";
var w = (qpw = "eighteen") ? "dieciocho":"no caps";
document.getElementByld("demo").innerHTML = w + "";
}
</script>
<script>
function myFunction() {
var qpe = "blue";
var e = (qpe = "blue") ? "azul":"no caps";
document.getElementByld("demo").innerHTML = e + "";
}
</script>
</body>
</html>
You are attempting to use the wrong equals sign for boolean logic:
var w = (qpw = "eighteen") ? "dieciocho":"no caps";
should be:
var w = (qpw == "eighteen") ? "dieciocho":"no caps";
or more, appropriately:
var w = (qpw === "eighteen") ? "dieciocho":"no caps";
Here is a full explanation on the usage of the equals: Does it matter which equals operator (== vs ===) I use in JavaScript comparisons?
There are a number of typos in your code that, I assume, are just simple mistakes, I put together a fiddle of your issue and solution.
I removed the inline javascript declaration from your html and added an id to the button:
<input id="x"value=""/>
<button id="btn">Try it</button>
<p id="demo"></p>
I then set the click event handler in javascript to the button:
document.getElementById('btn').onclick = myFunction;
I then removed the double functions and created one single function that checks against the user input:
function myFunction() {
var userData = document.getElementById('x').value;
var qpe = "blue";
var qpw = "eighteen";
var e = (userData == qpe) ? "azul": (userData == qpw)? "dieciocho" :"no caps";
document.getElementById("demo").innerHTML = e + "";
}
You have multiply errors in your code:
First of all:
<button on Click="myFunction():">Try it</button>
Should have been: (See the attribute name and the colon->semicolon)
<button onclick="myFunction();">Try it</button>
Secondly you cannot define two functions with the same name, the last will simply just override the first defined one... Which means all of this code is not used:
<script>
function myFunction() {
var qpw = "eighteen";
var w = (qpw = "eighteen") ? "dieciocho":"no caps";
document.getElementByld("demo").innerHTML = w + "";
}
</script>
Thirdly you one line if statement is wrong:
var e = (qpe = "blue") ? "azul":"no caps";
Should have been: (two equal signs are used for loose comparison (Three equal signs are used for strict comparison. For instance "1" == 1 but "1" !== 1))
var e = (qpe == "blue") ? "azul":"no caps";
Also there is not html element called <imput> I think you meant <input>:
<input id="x"value=""/>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
<script type='text/javascript'>
function replaceNewlines(e) {
var element = document.getElementById('myTextarea');
if (e.keyCode == 13) {
element.value = element.value.replace("\r", "<br />");
element.value += "___NEWLINE___\n";
element.value = element.value.replace("___NEWLINE___\n", "<br />");
}
}
</script>
This code works fine but when I press enter, <br /> appears in the textarea and I don't want to appear this in my textarea.
Can somebody help me?
So, how about replacing <br /> with a new line?
<script type='text/javascript'>
function replaceNewlines(e) {
var element = document.getElementById('myTextarea');
element.value = element.value.replace(/<br\s*\/?>/g, '\n');
}
</script>
See http://jsfiddle.net/tkR9S/ for a full demo.