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");
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>
My purpose is to check if value entered by user in the input field is already available in an array stored in the localstorage. If yes, print the array if not add the new value in the storage. I am getting my array back on button click but the code isn't working correctly. The output is:
["hh", "try", "vogue", "vogue", "try2", "try2", "try2", "try2"]
Above are the entered values which are getting added repetitively. I know it's a stupid issue but have least experience with handling arrays in localstorage. Any help would be appreciated. (I tried the solutions provided in similar questions on stackoverflow but no luck)
<html class=" reevoomark_ATY">
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Special Offers</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="crossorigin="anonymous"></script>
</head>
<body>
<input id="coupon"> <button style="width:50px;padding:10px;background:grey;height:30px;border:1px solid grey" id="button">Apply</button>
<input id="stored">
<script>
var coupons = ['hh'];
var storedNames;
localStorage.setItem("coupons", JSON.stringify(coupons));
$('#button').on('click', function(){
storedNames = JSON.parse(localStorage.getItem("coupons"));
var enteredValue = $('#coupon').val();
for(i=0; i <storedNames.length; i++) {
if(enteredValue === storedNames[i]) {
console.log("value Exist!!");
console.log(storedNames[]);
}
else {
console.log("in else");
coupons.push(enteredValue);
localStorage.setItem("coupons", JSON.stringify(coupons));
storedNames = JSON.parse(localStorage.getItem("coupons"));
}
}
});
</script>
</body>
</html>
You would like to push the names if it doesn't exist in the stored names. Refer attached code.
<html class=" reevoomark_ATY">
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Special Offers</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="crossorigin="anonymous"></script>
</head>
<body>
<input id="coupon"> <button style="width:50px;padding:10px;background:grey;height:30px;border:1px solid grey" id="button">Apply</button>
<input id="stored">
<script>
var coupons = ['hh'];
localStorage.setItem("coupons", JSON.stringify(coupons));
$('#button').on('click', function(){
var storedNames = JSON.parse(localStorage.getItem("coupons"));
var enteredValue = $('#coupon').val();
if (storedNames.includes(enteredValue)) {
console.log("value Exist!!");
console.log(storedNames);
} else {
console.log("in else");
storedNames.push(enteredValue);
localStorage.setItem("coupons", JSON.stringify(storedNames));
}
});
</script>
</body>
</html>
I have an unknown symbols displayed in my javascript application for try catch construction using czech language even when I use coding windows-1250. These symbols is displayed like question marks in diamond.
html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1250"/>
<title>Konstrukce Try/Catch</title>
<script type="text/javascript" src="number.js"></script>
</head>
<body>
<form name="formular" id="formular" action="#">
<div id="cisloDiv">Zadejte ΔΓslo v rozsahu 1 aΕΎ 100: <input id="cislo" name="cislo"> <span id="informace"> </span></div>
<div><input id="odeslatFormular" type="submit"></div>
</form>
<script type="text/javascript">
function inicializuj() {
document.forms[0].onsubmit = function() { return zkontrolujFormular(this) };
}
window.onload = inicializuj;
</script>
</body>
</html>
javascript
function zkontrolujFormular() {
try {
var cislo = document.forms[0]["cislo"];
if (isNaN(cislo.value)) {
var chyba = new Array("NejednΓ‘ se o ΔΓslo",cislo);
throw chyba;
}
else if (cislo.value > 100) {
var chyba = new Array("ZadanΓ© ΔΓslo je vΔtΕ‘Γ jak 100",cislo);
throw chyba;
}
else if (cislo.value < 1) {
var chyba = new Array("ZadanΓ© ΔΓslo je menΕ‘Γ jak 1",cislo);
throw chyba;
}
return true;
}
catch(objektVyjimky) {
var informace = document.getElementById("informace");
var textChyby = document.createTextNode(objektVyjimky[0]);
var novySpan = document.createElement("span");
novySpan.appendChild(textChyby);
novySpan.style.color = "#FF0000";
novySpan.style.fontWeight = "bold";
novySpan.setAttribute("id","informace");
var rodic = informace.parentNode;
rodic.replaceChild(novySpan,informace);
objektVyjimky[1].style.background = "#FF0000";
return false;
}
}
Have you tried using UTF-8 encoding?
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
or shorter:
<meta charset="utf-8" />
Nowadays using encoding other that UTF-8 is rather rare. You may need it in special circumstances, but whenever you can try to use UTF.
Now I have the solution, something was bad in my code because in solution (it is from learning material) it works.
Hello All i started working on html recently,but iam struck in this situation where i want to send a value from one html page to the next html page just like,how websites shows our name after sign up success.Here i have written two html pages with,
pageOne.html
<!DOCTYPE html>
<head>
<title>
Home
</title>
<meta charset="UTF-8">
</head>
<style>
.myP{
color: lightgreen;}
</style>
<body>
<p id="p1" class="myP" onclick="myFun()">DataSend</p>
</body>
<script>
function myFun(){
document.getElementById("p1").style.color = "blue";
var textprevi=document.getElementById("p1").innerHTML;
localStorage.setItem("message", textprevi);
window.open("pageTwo.html","_self");
}
</script>
</html>
and my second
pageTwo.html
<!DOCTYPE html>
<html>
<body onload="fun">
<input type="text" id="tBox">
</body>
<script>
function fun()
{
document.getElementById("tBox").innerHTML = localStorage.getItem("message");
}
</script>
</html>
But when i tried the above solution,the element with id = "tBox" was empty but i wanted it to be filled with value = "DataSend" which is from pageOne.html.
Please help me with the promblem.
Thanks in advance.
The problem is with this line
document.getElementById("tBox").innerHTML = localStorage.getItem("message");
Here tBox is a an input element. So you have to use value instead of innerHTML
document.getElementById("tBox").value= localStorage.getItem("message");
You can do it using querystring parameters
In page one:
function myFun(){
document.getElementById("p1").style.color = "blue";
var textprevi=document.getElementById("p1").innerHTML;
window.open("pageTwo.html?data=" + textprevi,"_self");
}
In page 2
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var data = getParameterByName('data');
You made two mistakes in pageTwo.html.
<!DOCTYPE html>
<html>
<body onload="fun()"><!-- You didn't invoke the function here -->
<input type="text" id="tBox">
</body>
<script>
function fun()
{
document.getElementById("tBox").value = localStorage.getItem("message"); // You need to set the value for input fields, not innerHTML.
}
</script>
</html>
These changes should make your code work.
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.