Passing String between two javascript Function - javascript

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
var ts='';
function test(value1)
{
// this works when the value1 is a number only but if value1 is a string is does not work
var optionsVal = ' ';
optionsVal= '<input type="button" onclick="postRun('+value1+')" value="test" /> ';
document.getElementById('test').innerHTML = optionsVal;
}
function postRun(km)
{
alert(km);
}
</script>
</head>
<body>
<%
String ss="Click Me";
%>
<input type="button" onclick="test('<%=ss%>')" value="Click me" />
<div id="test"></div>
</body>
</html>

Yes, this is because your embedded code is not escaped in the output. Use JSON:
optionsVal= '<input type="button" onclick=\'postRun('
+ JSON.stringify(value1)
+ ')\' value="test" />';

Related

Script keeps on saving the value even when unchecked

I want the script to save the value of the text only when it's checked.
If possible I also want to save the value of it only when it's clicked as a variable so I can do this for example
Name: {here the variable} #if checked
<html>
<head>
<title>Feedback</title>
<script language="Javascript" >
function download(filename, text) {
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' +
encodeURIComponent(text));
pom.setAttribute('download', filename);
pom.style.display = 'none';
document.body.appendChild(pom);
pom.click();
document.body.removeChild(pom);
}
function addTextHTML()
{
document.addtext.name.value = document.addtext.name.value + ".html"
}
function addTextTXT()
{
document.addtext.name.value = document.addtext.name.value + ".txt"
}
</script>
</head>
<body>
<form name="addtext" onsubmit="download(this['name'].value, this['text'].value)">
<input type="checkbox" name="text" id="test" value=name>
<label for="test">Test</label>
<br>
<input type="text" name="name" value="" placeholder="File Name">
<input type="submit" onClick="addTextHTML();" value="Save As HTML">
<input type="submit" onClick="addTexttxt();" value="Save As TXT">
</form>
</body>
</html>
Try to use break;. But it will give error and you'll be able to see it on console.
So try something like this;
var text = "";
var a;
for (a = 1; a < 2; a++){
//code will be here which gets in the loop
text += "Working";
}
document.getElementById("demo").innerHTML = text;
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width = device-width, initial-scale = 1.0">
<meta http-equiv="X-UA-Compatible" content = "ie=edge">
<title> Keat </title>
</head>
<body>
<p id="demo"></p>
</body>
</html>
This will make loop to but it will restrict the loop.

Ajax script is not updating PHP file and his var value

This is simple form, on click i need calculated value to be displayed in file process.php, but this is not working for me. It seems that "click" on "=" don't do anything, in process.php there is default value "0", no results.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<script src="//code.jquery.com/jquery-1.12.0.min.js">
$('#button').click(function() {
var val1 = $('#text1').val();
var val2 = $('#text2').val();
$.ajax({
type: 'POST',
url: 'demo/process.php',
data: { text1: val1, text2: val2 },
success: function(response) {
$('#result').html(response);
}
});
});
</script>
<input type="text" id="text1"> +
<input type="text" id="text2">
<button id="button"> = </button>
<div id="result"></div>
</body>
</html>
AND PHP
<?php
$text1 = $_POST['text1'];
$text2 = $_POST['text2'];
echo $text1 + $text2;
?>
first of all, are you using a php server?
if you do then try this (yes i used a different library):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<form>
<input type="text" id="text1" />
<input type="text" id="text2" />
<button type="button" id="button"> = </button>
</form>
<div id="result"></div>
<script type="text/javascript">
$('#button').click(function() {
var val1 = $('#text1').val();
var val2 = $('#text2').val();
$.post( "demo/process.php", {text1: val1, text2: val2} , function(data){
$('#result').html(data);
});
});
</script>
</body>
</html>
in php:
<?php
$text1 = $_POST['text1'];
$text2 = $_POST['text2'];
echo $text1 + $text2;
?>
Tip: Always specify the type attribute for a element. Different browsers use different default types for the element.

Displaying form data to new tab

I want display the form data to new tab using JavaScript only. This is not proceeding my task. How to achieve this?
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<script language="JavaScript">
function showInput() {
var message_entered = document.getElementById("user_input").value;
document.getElementById('display').innerHTML = message_entered;
}
</script>
</head>
<body>
<form>
<label><b>Enter a Message</b></label>
<input type="text" name="message" id="user_input">
</form>
<input type="submit" onclick="showInput();"><br />
<label>Your input: </label>
<p><span id='display'></span> </p>
</body>
</html>
var data = "<p>This is new tab'</p><br/>"+message_entered;
newWindow = window.open("data:text/html," + encodeURIComponent(data),
"_blank", "width=200,height=100");
newWindow.focus();
DEMO

Javascript to clean html codes and use alphanumeric only in return

i need a little help, figuring out my javascript output format
<!doctype html>
<html><head>
<meta charset="utf-8">
<title>cleaner</title>
<script language="javascript" type="text/javascript">
function show (text){
var userInput = document.getElementById('textbox').value;
userInput = userInput.replace(/\W/g, '');
userInput = userInput.replace(/ /gi,", ");
document.getElementById('text').innerHTML = userInput;
}
</script>
</head>
<body>
<input type='textbox' name='textbox' id='textbox' value="Title Here"/>
<input type=button name=button value="OK" onClick="show()"/>
<div id="text"></div></body></html>
my input
& ^A % - _ ^ % clean* this$ keyword-
my result is
A_cleanthiskeyword
i want a result like this
a, clean, this, keyword
what should i add in my replace code?
thanks
Please check this code this will works for you
<!doctype html>
<html><head>
<meta charset="utf-8">
<title>cleaner</title>
<script language="javascript" type="text/javascript">
function show ()
{
var userInput = document.getElementById('textbox').value;
var num = userInput.indexOf('A');
userInput = userInput.match(/[a-z]+/gi).join(", ").toLowerCase().replace(/ /g, '');
document.getElementById('text').innerHTML = userInput.substring(num-1);
}
}
</script>
</head>
<body>
<input type='textbox' name='textbox' id='textbox' value="Title Here"/>
<input type=button name=button value="OK" onClick="show()"/>
<div id="text"></div>
</body></html>
Here is link for JSFiddle for your code jsfiddle
Try this:
function show (text){
var userInput = document.getElementById('textbox').value;
userInput = userInput.match(/[a-z]+/gi).join(", ").toLowerCase();
document.getElementById('text').innerHTML = userInput;
}

Clear the output area

I have here a working code that lets the user input something and it displays the output right away, but I want to create a function triggered by a button that would clear the output area itself. Is this possible with my code?
Here is my complete code:
<html>
<meta charset="UTF-8">
<head>
<title>Sample Array</title>
<script>var index = 0;</script>
</head>
<body>
Input:
<input type="text" id="input">
<input type="button" value="GO" onClick="press(input.value)">
<p id = "display">
<script>
var sample = new Array();
function press(Number)
{
if ( Number != '' )
{
sample[index] = Number;
document.getElementById("display").innerHTML += sample[index] + "<br>";
index++;
}
else
alert("empty");
document.getElementById('input').value = '';
}
</script>
</body>
</html>
Try this,
Javascript:
function Clean(){
document.getElementById('display').innerHTML='';
}
HTML:
<input type="button" value="Clean" onClick="Clean()"/>
Do this:
Notes:
-I have closed the P element.
-A new button was added "Clean" and you can use JQuery as it is shown below to clean the output (remember to have JQuery referenced in your page).
<html>
<meta charset="UTF-8">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<title>Sample Array</title>
<script>var index = 0;</script>
</head>
<body>
Input:
<input type="text" id="input" />
<input type="button" value="GO" onClick="press(input.value)"/>
<input type="button" value="Clean" onClick="$('#display').html('');"/>
<p id = "display"></p>
<script>
var sample = new Array();
function press(Number)
{
if ( Number != '' )
{
sample[index] = Number;
document.getElementById("display").innerHTML += sample[index] + "<br>";
index++;
}
else
alert("empty");
document.getElementById('input').value = '';
}
</script>
</body>

Categories

Resources