whats wrong with my code?The result shows DemoUser as undefined.heres what have i done
<!DOCTYPE html>
<html>
<head>
</head>
<body>
Name : <span id="pname">DemoUser</span><button type="button" onclick="setname()">Edit</button>
<script type="text/javascript">
var nme;
document.getElementById("pname").innerHTML=nme;
function setname(){
nme = prompt("Enter your name","");
if (nme != "" && nme != ""){
setCookie("player",nme,300);
}
}
</script>
</body>
</html>
You need to make assign it after you get the value. Also, you're not initializing the variable, which is why it returns undefined also the if condition can be simplified a bit.
function setname() {
var nme = prompt("Enter your name", "");
if (nme) {
document.getElementById("pname").innerHTML = nme;
setCookie("player", nme, 300);
}
}
Related
So I am currently trying to find out how to select text between two characters(for the example I will use a slash / )
Here is what I have so far.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function startWhenLoaded() {
var text = $("p").text();
var secondStartingPoint, startPointOne, startPointTwo;
if (text.indexOf("/", 0) !== -1) {
//I did -1 because indexOf returns -1 if nothing is found.
/*Also, the second argument in indexOf() acts as my starting
point for searching.*/
secondStartingPoint = text.indexOf("/") + 1;
startPointOne = text.indexOf("/") + 1;
if (text.indexOf("/", secondStartingPoint) !== -1) {
startPointTwo = text.indexOf("", secondStartingPoint) + 1;
var selectedText = slice(startPointOne, startPointTwo);
$("body").append("<p>" + selectedText + "</p>");
//but nothing happens.
}
}
</script>
</head>
<body onload="startWhenLoaded()">
<p>/can I select and duplicate this?/</p>
</body>
</html>
But it doesn't do anything.
It could be achieved simply by using a regex like :
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function startWhenLoaded() {
var text = $("p").text();
var extracted = text.match(/\/(.*)\//).pop();
alert('The extracted text is :'+extracted);
}
</script>
</head>
<body onload="startWhenLoaded()">
<p>Some text here in the start /can I select and duplicate this?/ Some extra text at the end</p>
</body>
</html>
Regex is simplest and easiest way to get your solution.
use exec() function to get text between '/';
console.log(/^\/(.*)\/$/.exec('/some text, /other example//'))
I'm trying out this code and it's not working please help!
Here's the code:
var test = "3";
function printIsNaN(value) {
var bool = document.getElementById("booleanValue");
if (isNaN(value) === true) {
bool.style.color = "green";
bool.innerHTML = "true";
} else if (isNaN(value) === false) {
bool.style.color = "red";
bool.innerHTML = "false";
}
}
printIsNaN(test);
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title>Stackoverflow Question</title>
<script src="something.js"></script>
</head>
<body>
<!-- START OF THE DOCUMENT -->
<span id="booleanValue"></span>
</body>
</html>
Thanks if you can help PLEASE!!!!!!!!
you could use ParseInt() to change the string into an integer
function printIsNaN(value) {
var bool = document.getElementById("booleanValue");
if (isNaN(parseInt(value))) {
console.log("nope");
} else if (!isNaN(parseInt(value))) {
console.log("yep");
}
}
hope this helps
In ES6 there is a function on the Number object called isNaN it test the value if it's a true NaN (Number and not a string)
You can implement that function like this:
if (!Number.isNaN) {
Number.isNaN = function(n) {
return n !== n;
};
}
It relies on the face that NaN is the only value that doesn't equal to itself.
Source: https://github.com/getify/You-Dont-Know-JS/blob/master/types%20%26%20grammar/ch2.md
I need to access in a different .js file the value inside $generatedP and display it
$(document).ready(function() {
var $buttonValue = $(".value_generate");
var $divValue = $(".generated_value");
var $generatedP = $(".generated_p");
var $valueInput2 = $(".value_input_2");
var $submitPages2 = $(".submit_pages_2");
function valueGenerator(value) {
var valueString="";
var lettersNumbers = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for(var i = 0; i < value; i++)
valueString += lettersNumbers.charAt(Math.floor(Math.random()* lettersNumbers.length));
return valueString;
}//generate string
$buttonValue.click(function generate() {
var $key = valueGenerator(12);
$generatedP.html($key);//display generated string
});
$submitPages2.click(function() {
if($valueInput2.val() == $generatedP.text() ){
alert("you are logged in website");
} else {
alert("please check again the value");
return false;
}//check value if true/false
});
I am new to jquery
You have a few options.
Create a namespace inside the jQuery object:
$.myGlobalNamespace = {};
$.myGlobalNamespace.generatedPvalue = "something";
Define an object at the window level:
window.myGlobalNamespace = {};
window.myGlobalNamespace.generatedPvalue = "something";
Just be sure to use a sensible name for the namespace object.
You can improve the behavior doing client-side checking with localStorage, or you can simply use sessionStorage. Variable $generatedP will be available in page1 and page2. Hope it helps!
PAGE 1:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<script type = "text/javascript">
$(document).ready(function(){
var $generatedP = "27.23.10";
sessionStorage.setItem('myVar', $generatedP);
window.location.href = "page2.html";
});
</script>
</body>
</html>
PAGE 2: to access the variable just use the getItem method and that is all.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
var data = sessionStorage.getItem('myVar');
alert(data);
</script>
</body>
</html>
I'm trying to make a program where if you for example type in "less" in the textarea the output should show "<". What is the best way to do this?
This is how far I've gotten:
<!doctype html>
<html lang="en">
<head>
<title>Group 7 - Deckcode to JavaScript</title>
</head>
<body>
<h1>Group 7 - Deckcode to JavaScript</h1>
<p>Input your deckode below:</p>
<textarea id="myTextarea"></textarea>
<button type="button" onclick="myFunction()">Translate</button>
<p id="demo"></p>
<script>
function myFunction() {
var input
if (myTextarea == "less") {
console.log("<");
}
}
</script>
</script>
</body>
</html>
You're trying to fish values out of the DOM incorrectly. Use document.getElementById to locate the element in the DOM, and take its value for the value you require.
function myFunction() {
var textAreaValue = document.getElementById("myTextarea").value;
if (textAreaValue == "less") {
console.log("<");
}
}
I suggest using an object like this:
var translations = {};
translations["less"] = "<";
translations["greater"] = ">";
And then in your function you do like this:
function myFunction() {
var value = document.getElementById("myTextarea").value;
console.log(translations[value] ? translations[value] : "No translation found");
}
It would also be easy to add more translations e.g. based on data from a database or similar.
as and alternative to IF-condition, you can use
if (textAreaValue.indexOf("less") > -1) {
console.log("<");
}
so if the text area contains "less" text then the console prints "<"
indexOf method
This is so bloody frustrating. I went through W2school tutorials, where bits and pieces are presented to you but it does not answer how to put it all together. I thought I understood it but when I put it into practice, nothing. Firebug is telling me that inputEmp() is not defined, but is quite obviously defined in the .js file. Can someone please tell me what minor detail I left out? Thanks First the Html, then the .js file.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/payroll.css" />
<title>Payroll System</title>
<script type="text/javascript" src="scripts/payroll.js"></script>
<script type="text/javascript" >
var emps = new Array();
</script>
</head>
<body>
<h1>Jackson Payroll System</h1>
<button type="button" onclick="inputEmp()">
Click here to enter employees
</button>
</body>
</html>
// payroll.js
function inputEmp() {
var inName = "";
var loopCt = 0
var tArray = new Array();
while (inName != "-1}
{
prompt inName = prompt("Please enter your name (enter -1 to finish)",
"Employee Name");
if (inName == "-1") { break; }
if (inName==null || inName=="")
{
alert("Blank names are not allowed. To exit enter '-1'.");
}
else
tArray[loopCt++] = inName;
{
}
return tArray;
}
Yes, you forgot to close the brace { of while loop before return statement. and the quotes is not closed in condition.
Try this
function inputEmp() {
var inName = "";
var loopCt = 0
var tArray = new Array();
while (inName != "-1"} {
prompt inName = prompt("Please enter your name (enter -1 to finish)","Employee Name");
if (inName == "-1") { break; }
if (inName==null || inName=="")
{
alert("Blank names are not allowed. To exit enter '-1'.");
}
else
tArray[loopCt++] = inName;
{
}
}
return tArray;
}
There is a lot of problem in your code:
var loopCt = 0 should look like this var loopCt = 0;
while (inName != "-1} should look like this while (inName != "-1)
prompt inName = prompt... should look like this inName = prompt...
There is an empty { } under the else statement
Here is a jsfiddle that shows you the corrected code.