I am new to Json. please tell me how to access following element. this is my Json
var data = {"student":[{"fname":"mad", "sname":"cha"}, {"fname":"sun", "sname":"ban"}, {"fname":"sha", "sname":"kiri"}]};
so how I access to the "mad"value.
actually this is what I trying to do
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script>
function click_me(){
var data = {"student":[{"fname":"mad", "sname":"cha"}, {"fname":"sun", "sname":"ban"}, {"fname":"sha", "sname":"kiri"}]};
localStorage.setItem('my_name',data);
alert('OK');
}
function show_me(){
var x = localStorage.getItem('my_name');
x = x.student[0].fname;
alert(x);
}
</script>
</head>
<body>
<input type="button" name="btn" value="Click" onclick="click_me()">
<input type="button" name="btn" value="show_me" onclick="show_me()">
</body>
</html>
It's
var yourValue = data.student[0].fname
By the way, this isn't JSON but a plain javascript object. JSON is a string based interchange format.
EDIT following your edit.
localStorage only stores strings. That's why getItem gives you a string in which you can't find your "mad".
Here's what you can do :
function click_me(){
var data = {"student":[{"fname":"mad", "sname":"cha"}, {"fname":"sun", "sname":"ban"}, {"fname":"sha", "sname":"kiri"}]};
localStorage.setItem('my_name', JSON.stringify(data));
alert('OK');
}
function show_me(){
var x = JSON.parse(localStorage.getItem('my_name')||"{}");
x = x.student[0].fname; // it would be cleaner to test x.student exists
alert(x);
}
Access the value as follows:
var firstName = data.student[0].fname;
Example: http://jsfiddle.net/N8bSk/
you can do this by
var yourValue = data.student[0].fname
Related
The result of the following code is only π§π½ββοΈ πΎπΏπππ»πΌπ§π€ππ©π«π¨π¬, where I want to find all the unique graphic represationations shown in the Firefox output that I copied to the var a originally. (Basically the code should show the original a string as output.)
How to do that?
<html>
<head>
<meta charset="UTF-8" />
<script>
var a = 'π§π½ββοΈ π§πΎββοΈ π§πΏββοΈ π§ππΏ ππ» ππΌ ππ½ ππΎ ππΏ π§π»βπ€βπ§π» π§π»βπ€βπ§πΌ π§π»βπ€βπ§π½ π§π»βπ€βπ§πΎ π§π»βπ€βπ§πΏ π§πΌβπ€βπ§π» π§πΌβπ€βπ§πΌ π§πΌβπ€βπ§π½ π§πΌβπ€βπ§πΎ π§πΌβπ€βπ§πΏ π§π½βπ€βπ§π» π§π½βπ€βπ§πΌ π§π½βπ€βπ§π½ π§π½βπ€βπ§πΎ π§π½βπ€βπ§πΏ π§πΎβπ€βπ§π» π§πΎβπ€βπ§πΌ π§πΎβπ€βπ§π½ π§πΎβπ€βπ§πΎ π§πΎβπ€βπ§πΏ π§πΏβπ€βπ§π» π§πΏβπ€βπ§πΌ π§πΏβπ€βπ§π½ π§πΏβπ€βπ§πΎ π§πΏβπ€βπ§πΏ ππ» π©π»βπ€βπ©πΌ π©π»βπ€βπ©π½ π©π»βπ€βπ©πΎ π©π»βπ€βπ©πΏ π©πΌβπ€βπ©π» ππΌ π©πΌβπ€βπ©π½ π©πΌβπ€βπ©πΎ π©πΌβπ€βπ©πΏ π©π½βπ€βπ©π» π©π½βπ€βπ©πΌ ππ½ π©π½βπ€βπ©πΎ π©π½βπ€βπ©πΏ π©πΎβπ€βπ©π» π©πΎβπ€βπ©πΌ π©πΎβπ€βπ©π½ ππΎ π©πΎβπ€βπ©πΏ π©πΏβπ€βπ©π» π©πΏβπ€βπ©πΌ π©πΏβπ€βπ©π½ π©πΏβπ€βπ©πΎ ππΏ π«π» π©π»βπ€βπ¨πΌ π©π»βπ€βπ¨π½ π©π»βπ€βπ¨πΎ π©π»βπ€βπ¨πΏ π©πΌβπ€βπ¨π» π«πΌ π©πΌβπ€βπ¨π½ π©πΌβπ€βπ¨πΎ π©πΌβπ€βπ¨πΏ π©π½βπ€βπ¨π» π©π½βπ€βπ¨πΌ π«π½ π©π½βπ€βπ¨πΎ π©π½βπ€βπ¨πΏ π©πΎβπ€βπ¨π» π©πΎβπ€βπ¨πΌ π©πΎβπ€βπ¨π½ π«πΎ π©πΎβπ€βπ¨πΏ π©πΏβπ€βπ¨π» π©πΏβπ€βπ¨πΌ π©πΏβπ€βπ¨π½ π©πΏβπ€βπ¨πΎ π«πΏ π¬π» π¨π»βπ€βπ¨πΌ π¨π»βπ€βπ¨π½ π¨π»βπ€βπ¨πΎ π¨π»βπ€βπ¨πΏ π¨πΌβπ€βπ¨π» π¬πΌ π¨πΌβπ€βπ¨π½ π¨πΌβπ€βπ¨πΎ π¨πΌβπ€βπ¨πΏ π¨π½βπ€βπ¨π» π¨π½βπ€βπ¨πΌ π¬π½ π¨π½βπ€βπ¨πΎ π¨π½βπ€βπ¨πΏ π¨πΎβπ€βπ¨π» π¨πΎβπ€βπ¨πΌ π¨πΎβπ€βπ¨π½ π¬πΎ π¨πΎβπ€βπ¨πΏ π¨πΏβπ€βπ¨π» π¨πΏβπ€βπ¨πΌ π¨πΏβπ€βπ¨π½ π¨πΏβπ€βπ¨πΎ π¬πΏ π» πΌ π½ πΎ πΏ ';
var b = [...new Set([...a])];
var c = b.join('');
function init() { document.getElementById('result').innerHTML = c; }
</script>
</head><body onload="init()">
<span id="result"></span>
</body>
</html>
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 have this JS code, in which i would like to call x var and set value. But it is not working: When the function returns the value of x, the result is undefined:
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<script type="text/javascript">
var myvar={
FirstL:function(){
var x='hello world';
}
}
</script>
</head>
<body>
<input type="button" value="click me" onclick="document.write(myvar.FirstL().x);" >
</body>
</html>
var x is a private variable and you cannot use it outside the function scope, you may just return the x and call your method:
var myvar={
FirstL:function(){
var x='hello world';
return x;
}
}
In html attribute:
onclick="document.write(myvar.FirstL());"//FirstL() returns the 'x'
Suggestion:
Don't use document.write() you may google for its alternative.
Firstly this may sound like a duplicate question but I was unable to resolve my problem using the previous asked questions:
This is my HTML page:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script>
function test() {
var totalids='XYZ##';
var variable_name=document.getElementById('p1').value.replace(/^\s+|\s+$/g,'');
var result = totalids.match(/variable_name/i);
if (result){
alert('Matched');
} else {
alert("Not Matched");
}
}
</script>
</head>
<body>
<input type="button" value="click on me" onclick="test()">
<input type="text" value="xyz" id="p1">
</body>
</html>
This program is not matching my String.I want to match my string using case-insensitive match.I know the problem lies in this line:
var result = totalids.match(/variable_name/i);
I am unable to pass variable in match function.I can do this;
var re = new RegExp(str1, "g");
But I don't know how to do cases-insensitive search using above line.
Here is how:
var re = new RegExp(str1, "gi");
I have an html form and a js script that gets the value of a textarea. However, when I'm getting the value of the textarea with javascript it return "undefined".
I have the following:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="js/jquery/jquery-1.9.1.min.js"></script>
<script src="contentForm.js"></script>
</head>
<body>
<form method="post" >
<textarea name ="editor" id="editBox" rows="5" cols="2">type</textarea>
<p><input type="submit" id="submit" value="Submit"></p>
</form>
</body>
</html>
JS
function add() {
$("#submit").click(function() {
document.write("hello motto");
var message = "start js";
console.log(message);
var contents = $("#editBox");
var a = contents.val();
if(a === undefined) {
console.log("contents undefined");
}
console.log(contents);
var item = {"id":"12", "content": contents};
var obj = JSON.stringify(item);
var obj2 = JSON.parse(obj);
console.log("you have arrived");
document.write(obj2.id);
});
}
$(document).ready(function () {
add();
});
I tried the following to get the value of the textarea:
var content = $("#editBox").val();
and
var contents = $("textarea#editBox").val();
The val of the textarea is always undefined, with each of the methods I tried.
Is there another method for retrieving the value of a textarea?
Running document.write on a closed document will open a new document and destroy the DOM of the existing one.
This destroys the textarea, so when you try to retrieve it to get the value, it does not exist.
Remove the line document.write("hello motto");.
Use DOM manipulation to edit the existing document, instead of writing a new one.