Hide <br /> tag from textarea [closed] - javascript

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.

Related

How to place a variable inside getElementbyId() that changes during each repetition of a loop? [closed]

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>

getting error when trying to change asp.net label values using JavaScript [closed]

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 8 years ago.
Improve this question
I have an asp.net website. I want to change the label values in a JavaScript function.
Here is my code,
<td id="m1" runat"server" onclick = "createChart_Jan"></td>
<asp:Label ID="lblGeneral" runat="server" Text="" Font-Bold="true" Font-Size="Medium"></asp:Label>
JavaScript,
function createChart_Jan() {
var month = 1;
var d = new Date();
var month_current = d.getMonth() + 1;
if(month > month_current)
{
document.getElementbyId("lblGeneral").innerHTML = "0";
}
else
{
document.getElementbyId("lblGeneral").innerHTML = "500";
}
}
But I am getting "Uncaught TypeError: undefined is not a function" error when I click on td element.
Any idea on what is wrong with above code?
You have few errors -
Calling function from click should be onclick="createChart_Jan();"
If you want to call server-side control's ID from client-side, you want to use lblGeneral.ClientID.
You have typo in getElementById (getElementbyId is incorrect)
Fix
<td id="m1" runat="server" onclick="createChart_Jan();">test</td>
<asp:Label ID="lblGeneral" runat="server" Text="" Font-Bold="true" Font-Size="Medium"></asp:Label>
<script>
function createChart_Jan() {
var month = 1;
var d = new Date();
var month_current = d.getMonth() + 1;
if (month > month_current) {
document.getElementById("<%= lblGeneral.ClientID %>").innerHTML = "0";
}
else {
document.getElementById("<%= lblGeneral.ClientID %>").innerHTML = "500";
}
}
</script>
How to debug
You can use browser's debugger to debug the JavaScript. For example, Chrome.
You Should try to get it by the ClientID and not by the serverID
something like:
document.getElementById('<%= lblGeneral.ClientID %>')

Need Help: Make the quiz random [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm having trouble making the questions random. If you could help, it would be awesome!
(If you have spear time; I've been given the task to mark correct answer with a green feather and wrong answers with red feather, for instance, if you get 3 correct and 2 wrong. It will show 3 green feathers and 2 red feathers as score.) Thank you!
<script type="text/javascript">
var questions = [
['firstcar.gif','0'],
['secondcar.gif','1'],
['thirdcar.gif','2'],
['firstcar.gif','0'],
['secondcar.gif','1'],
['thirdcar.gif','2'] // Note: no comma after last entry
];
var qNo = 0;
var correct = 0;
var cnt = 0;
function NextQuestion(response) {
if ((qNo < questions.length) && (response == questions[qNo][1])) {
correct++;
}
document.getElementById('score').innerHTML = 'Correct ' + correct + ' of 6 questions';
qNo++;
if (qNo < questions.length) {
document.getElementById('Pic').src = questions[qNo][0];
cnt++;
}else{
alert('Quiz is done. You got ' + correct + ' points!');
}
}
onload = function() {
document.getElementById('Pic').src = questions[0][0];
}
</script>
</head>
<body>
<div align="center">
<h1>Which car is it?</h1>
<img src="" id="Pic" height="200" width="250">
<p>Is it
<button onclick="NextQuestion('0')">Red</button>
<button onclick="NextQuestion('1')">Black</button>
<button onclick="NextQuestion('2')">Yellow</button>
<p>Your score: <br>
<span id="score"></span>
</div>
</body>
</html>
Well, your questions are in an array. So what you should be researching is how to randomise the order of an array.
questions.sort(function() { return Math.floor(Math.random() * 2); });

javascript split string by pattern [closed]

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.

How short can this script be and still achieve the same thing - display a message "loading.." followed by an extra period every 1 second for 7 seconds [closed]

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 8 years ago.
Improve this question
<script>
var dot = ".";
var counter = 0;
var message = "Loading Page";
function writeLoadingMessage(){
if (counter > 6) return;
setTimeout('writeMessage();',1000);
}
function writeMessage(){
document.getElementById('loadingMessageSpan').innerHTML = message + dot;
message += dot
counter++
writeLoadingMessage();
}
</script>
<BODY>
<span style="font-weight:bold;" id="loadingMessageSpan"></span>
<SCRIPT>writeLoadingMessage();</SCRIPT>
</BODY>
(function(){var i=6,a=setInterval(function(){document.getElementById('loadingMessageSpan').innerHTML+=".";!i--&&clearInterval(a);},1E3)})()
You will need to have Loading already in your span tag. As a note it is pointless to worry about getting it this small though. Also, the page will will have to be already loaded.
If you want it shorter: http://jsfiddle.net/pimvdb/wBFSy/.
<script>
var counter = 0;
function writeMessage() {
document.getElementById('loadingMessageSpan').innerHTML += ".";
if(++counter < 7) {
setTimeout(writeMessage, 1000);
}
}
window.onload = writeMessage;
</script>
<body>
<span style="font-weight:bold;" id="loadingMessageSpan">Loading Page</span>
</body>
Minifying also helps :)
var a=0;function b(){document.getElementById("loadingMessageSpan").innerHTML+=".";++a<7&&setTimeout(b,1E3)}window.onload=b
1) User setInterval instead.
2) Just set the message once and append . to end
3) Put all the js at the bottom
<body>
<span style="font-weight:bold;" id="loadingMessageSpan">Loading Page</span>
<script type="text/javascript">
var counter = 0;
var id = setInterval(function(){
document.getElementById('loadingMessageSpan').innerHTML += ".";
if(++counter>6) {
clearInterval(id);
}
}, 1000);
</script>
</body>
If you really want it short you can go this route:
<BODY>
<span style="font-weight:bold;" id="loadingMessageSpan">Loading Page</span>
<script>
var counter = 0;
var interval = setInterval(function(){
document.getElementById('loadingMessageSpan').innerHTML += ".";
if(++counter > 6)
clearInterval(interval);
},1000)
</script>
</BODY>
http://jsfiddle.net/kc3fb/2/

Categories