I have the following code, I'm trying to determine which button a user presses.
function Navigation(navigating, timed) {
if (navigating == general) {
if (timed) {
window.location.replace('general.html?timed=' + timed);
} else {
window.location.replace('general.html');
}
} else if (navigating == sport) {
if (timed) {
window.location.replace('sport.html?timed=' + timed);
} else {
window.location.replace('sport.html');
}
} else if (navigating == music) {
if (timed) {
window.location.replace('music.html?timed=' + timed);
} else {
window.location.replace('music.html');
}
}
}
<button type="button" id="general" onclick="Navigation(general, null)">General Knowledge</button>
<button type="button" id="sport" onclick="Navigation(sport, null)">Sport</button>
<button type="button" id="music" onclick="Navigation(music, null)">Music</button>
<button type="button" id="generalTimer" onclick="Navigation(general, 'true')">Timed General Knowledge</button>
<button type="button" id="sportTimer" onclick="Navigation(sport, 'true')">Timed Sport</button>
<button type="button" id="musicTimer" onclick="Navigation(music, 'true')">Timed Music</button>
It actually works, however when I get to the next page, I don't know how to get the timed variable to affect the page.
function getQueryVariable(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == variable){return pair[1];}
}
return(false);
}
Usage
Example URL:
http://www.example.com/index.php?id=1&image=awesome.jpg
Calling getQueryVariable("id") - would return "1".
Calling getQueryVariable("image") - would return "awesome.jpg".
Source: CSS-TRICKS
You can get the url params from window.location.search. This function will parse the values, and allow you to get them all or just a single value.
unfortunately it's not working in the debug window due to the ristrictions of the iframe.
function urlParams(key) {
const search = location.search.slice(1)
.split(/\=|\&/)
.filter(val => val !== '')
const obj = {}
for (let ii = 0; ii < search.length; ii+=2) {
obj[search[ii]] = search[ii + 1]
}
return key
? obj[key]
: obj
}
console.log(
urlParams('testing'), // get specific param
urlParams() // get all params
)
you can use localstorage
for example:
first save the value that has been choosen to localstorage by code like this
localStorage.nav = navigating;
and then
for nextpages and whereever you want call the value with
document.getElementById("result").innerHTML=localStorage.nav;
for example write down it with
Related
I'm writing the first item of an array to the screen, and would like to create Next/Previous buttons for array, but I can't get it to work. I have tried several methods, but I can't find suitable solution.
Can anyone help?
This is the last one I have tried:
var data = [
{"subject":"starcraft2",
"date":"08.31",
"dDay":"mon",
"content1":"STARCRAFT2",
"content2":"season2",
"playerA":"Ju",
"playerB":"Lee",
"emblemA":"Terran",
"emblemB":"Zerg",
"result":"end"},
{"subject":"starcraft2",
"date":"08.29",
"dDay":"wed",
"content1":"STARCRAFT2",
"content2":"season2",
"playerA":"kim",
"playerB":"joo",
"emblemA":"Terran",
"emblemB":"Protoss",
"result":"end"},
];
function prevAction() {
// function (e) { // the e here is the event itself
alert("Prev Click!");
// document.getElementById('subject').textContent = prevItem();
// document.getElementById('date').textContent = prevItem();
for (var i = 0; i<data.length; i++)
for (var j=0; j<data[i]; j++)
while(j === 0)
{
j == j++;
console.log(j);
}
console.log(data[j].date + ', ');
document.getElementById('date').textContent = data[j].date;
// document.getElementById('subject').textContent = j[0];
}
Here's the jist of how you'd accomplish this in pure Javascript:
getNextItem() {
var index = document.getElementById("index").value;
//add guards here to prevent array overflow/underflow
if (data.length < index - 1) {
index.value++;
}
document.getElementById("DOM_ELEMENT_TO_ATTACH_DATA").innerHTML = data[index];
}
getPreviousItem() {
var index = document.getElementById("index").value;
//add guards here to prevent array overflow/underflow
if (index > 0) {
index.value--;
}
document.getElementById("DOM_ELEMENT_TO_ATTACH_DATA").innerHTML = data[index];
}
<input id="index" type="hidden" name="index" value="0">
<button type="button" onclick="getNextItem()">Next Item</button>
<button type="button" onclick="getPreviousItem()">Previous Item</button>
Note that this will just attach the pure json data to a DOM element, it won't do anything fancy with it.
Furthermore, if you want to do anything more complex, I'd strongly recommend you look into using a library like jQuery or Angular as it's going to make your life a whole lot easier in the long run.
It's my first time to try angular in .NET with the use of local storage and I'm still trying to understand things. I hope someone will guide me here. Here are some snippet of the code I'm trying to do.
this is client.html:
<div class="box-footer">
<button type="submit" class="btn btn-primary btn-sm" data-ng-click="vm.save()"> Save</button>
</div>
this is clientService.js:
// Suppose to combine SAVE and EDIT but how?
function save(client) {
var clients = [];
if (localStorage.getItem('clients') != null)
clients = JSON.parse(localStorage.getItem('clients'));
clients.push(client);
localStorage.setItem('clients', JSON.stringify(clients));
}
function edit(client) {
var clients = [];
if (localStorage.getItem('clients') != null)
clients = JSON.parse(localStorage.getItem('clients'));
clients.forEach(function (client) {
//compare the two ID's, if they are the same
//Update
});
//save
localStorage.setItem('clients', JSON.stringify(clients));
}
this is client.js:
var vm = this;
vm.client = {
id: '',
firstname: '',
lastname: '',
age: ''
};
function edit() {
clientService.edit(vm.client);
getClients();
clear();
}
function save() {
clientService.save(vm.client);
getClients();
clear();
}
I expect to have a save button that can add a new data and update a data after selecting from the table.
if you are looking to update or save with same function try some thing like this
function edit(client) {
var clients = [];
var data = JSON.parse(localStorage.getItem('clients'));
if (data != null){
clients = data;
var position = data.indexOf(client);
/** checks if existing or not if then it will upadte same**/
if(position != -1){
clients[position] == client;
}
else{
clients.push(client)
}
}
else {
/** push first item**/
clients.push(client)
}
clients.forEach(function (client) {
//compare the two ID's, if they are the same
//Update
});
//save
localStorage.setItem('clients', JSON.stringify(clients));
}
You can use a ternary operator to check if there's a client upon submitting.
<button type="submit" class="btn btn-primary btn-sm" data-ng-click="vm.client ? vm.edit() : vm.save()">
<input type="button" name="name" value="Enero" id="mytext" onclick="my()">
function my() {
var poop = document.getElementById("mytext").value
if (poop = "Enero") {
poop = "Febrero"
} else if (poop = "Febrero") {
poop = "Marzo"
} else if (poop = "Marzo") {
poop = "Abril"
}
}
Hey I'm trying to make a function in an input button so that when you press click , the value of the button changes to different values depending of the clicks you give.
For example the default value is "Enero" (January in Spanish), so you click on in and its changes to "Febrero" consecutively until it returns to "Enero" and the cycle starts again.
(You can help me with a Jquery function or a Javascript function)
In javascript a single equals is an assignment, so you want to change all your if statements to ==or === instead like:
function my() {
var poop = document.getElementById("mytext").value
if (poop === "Enero") {
poop = "Febrero"
} else if (poop === "Febrero") {
poop = "Marzo"
} else if (poop === "Marzo") {
poop = "Abril"
}
}
Just fix the months spelling (idk Spanish so I guessed) and add the rest of them to the array..
function my() {
var poop = document.getElementById('poop');
var mnths = ['janurenao', 'februareano', 'marcheano', 'aprileona', 'may-eona'];
var index = mnths.indexOf(poop.value);
if (index > -1) {
index++;
if(index === mnths.length) index = 0;
poop.value = mnths[index];
}
}
<input value='janurenao' id='poop' />
<button onclick='my()'>pressme</button>
I have a problem with my Script. I want to do the following steps in this order:
1. Save the text in the input field.
2. Delete all text in the input field.
3. Reload the same text that was deleted before in the input field.
The problem with my script is that the ug()- function writes undefined in my textbox instead of the string that should be stored in var exput. The alert(exput) however shows me the correct content.
Help would be very much appreciated. And I'm sure there is better ways to do that, I'm quite new to this stuff.
HTML
<textarea id="a" style="width: 320px; height: 200px;"></textarea>
<input type="checkbox" id="remember" onclick="merker();deleter();ug()" />
Javascript
function merker() {
var merkzeug = document.getElementById('a').value;
ug(merkzeug);
};
function deleter() {
if(document.getElementById('remember').checked == true)
{
document.getElementById('a').value = "";
}
else {document.getElementById('a').value = "";
}
};
function ug(exput) {
alert(exput);
document.getElementById('a').value = exput;
};
Your code is calling merker(); deleter(); ug(); in the onclick event, but ug() is already called by merker(). You should be doing this instead:
function merker() {
var merkzeug = document.getElementById('a').value;
deleter();
ug(merkzeug);
};
function deleter() {
if(document.getElementById('remember').checked == true)
{
document.getElementById('a').value = "";
}
else {document.getElementById('a').value = "";
}
};
function ug(exput) {
alert(exput);
document.getElementById('a').value = exput;
};
<textarea id="a" style="width: 320px; height: 200px;"></textarea>
<input type="checkbox" id="remember" onclick="merker();" />
I changed Your Javascript:
function merker() {
merkzeug = document.getElementById('a').value;//global variable without var
ug();//why You use it here? I think only for test. So delete it after.
};
function deleter() {
if(document.getElementById('remember').checked == true)
{
document.getElementById('a').value = "";
}
else {document.getElementById('a').value = "";
}
};
function ug() {
alert(merkzeug);
document.getElementById('a').value =merkzeug;
};
Problems with your code:
method ug was used with argument and without argument ( i changed to without )
to restore deleted value it must be saved to some variable, i saved to global merkzeug variable - this is not good practice but sufficient in this case
next i used merkzeug to restore value in textarea in ug() function
i do not know why You using ug() two times? maybe delete one of them is good thing to do.
In plunker - https://plnkr.co/edit/fc6iJBL80KcNSpaBd0s9?p=info
problem is: you pass undefined variable in the last ug function:
you do: merker(value) -> ug(value); delete(); ug(/*nothing*/);
or you set your merkzeung variable global or it will never be re-inserted in your imput:
var merkzeug = null;
function merker() {
merkzeug = document.getElementById('a').value;
ug(merkzeug);
};
function deleter() {
if(document.getElementById('remember').checked == true)
{
document.getElementById('a').value = "";
}
else {document.getElementById('a').value = "";
}
};
function ug(exput) {
if (typeof exput === 'undefined') exput = merkzeung;
alert(exput);
document.getElementById('a').value = exput;
};
Am struggling hard to bind an array object with list of span values using watcher in Angularjs.
It is partially working, when i input span elements, an array automatically gets created for each span and when I remove any span element -> respective row from the existing array gets deleted and all the other rows gets realigned correctly(without disturbing the value and name).
The problem is when I remove a span element and reenter it using my input text, it is not getting added to my array. So, after removing one span element, and enter any new element - these new values are not getting appended to my array.
DemoCode fiddle link
What am I missing in my code?
How can I get reinserted spans to be appended to the existing array object without disturbing the values of leftover rows (name and values of array)?
Please note that values will get changed any time as per a chart.
This is the code am using:
<script>
function rdCtrl($scope) {
$scope.dataset_v1 = {};
$scope.dataset_wc = {};
$scope.$watch('dataset_wc', function (newVal) {
//alert('columns changed :: ' + JSON.stringify($scope.dataset_wc, null, 2));
$('#status').html(JSON.stringify($scope.dataset_wc));
}, true);
$(function () {
$('#tags input').on('focusout', function () {
var txt = this.value.replace(/[^a-zA-Z0-9\+\-\.\#]/g, ''); // allowed characters
if (txt) {
//alert(txt);
$(this).before('<span class="tag">' + txt.toLowerCase() + '</span>');
var div = $("#tags");
var spans = div.find("span");
spans.each(function (i, elem) { // loop over each spans
$scope.dataset_v1["d" + i] = { // add the key for each object results in "d0, d1..n"
id: i, // gives the id as "0,1,2.....n"
name: $(elem).text(), // push the text of the span in the loop
value: 3
}
});
$("#assign").click();
}
this.value = "";
}).on('keyup', function (e) {
// if: comma,enter (delimit more keyCodes with | pipe)
if (/(188|13)/.test(e.which)) $(this).focusout();
if ($('#tags span').length == 7) {
document.getElementById('inptags').style.display = 'none';
}
});
$('#tags').on('click', '.tag', function () {
var tagrm = this.innerHTML;
sk1 = $scope.dataset_wc;
removeparent(sk1);
filter($scope.dataset_v1, tagrm, 0);
$(this).remove();
document.getElementById('inptags').style.display = 'block';
$("#assign").click();
});
});
$scope.assign = function () {
$scope.dataset_wc = $scope.dataset_v1;
};
function filter(arr, m, i) {
if (i < arr.length) {
if (arr[i].name === m) {
arr.splice(i, 1);
arr.forEach(function (val, index) {
val.id = index
});
return arr
} else {
return filter(arr, m, i + 1)
}
} else {
return m + " not found in array"
}
}
function removeparent(d1)
{
dataset = d1;
d_sk = [];
Object.keys(dataset).forEach(function (key) {
// Get the value from the object
var value = dataset[key].value;
d_sk.push(dataset[key]);
});
$scope.dataset_v1 = d_sk;
}
}
</script>
Am giving another try, checking my luck on SO... I tried using another object to track the data while appending, but found difficult.
You should be using the scope as a way to bridge the full array and the tags. use ng-repeat to show the tags, and use the input model to push it into the main array that's showing the tags. I got it started for you here: http://jsfiddle.net/d5ah88mh/9/
function rdCtrl($scope){
$scope.dataset = [];
$scope.inputVal = "";
$scope.removeData = function(index){
$scope.dataset.splice(index, 1);
redoIndexes($scope.dataset);
}
$scope.addToData = function(){
$scope.dataset.push(
{"id": $scope.dataset.length+1,
"name": $scope.inputVal,
"value": 3}
);
$scope.inputVal = "";
redoIndexes($scope.dataset);
}
function redoIndexes(dataset){
for(i=0; i<dataset.length; i++){
$scope.dataset[i].id = i;
}
}
}
<div ng-app>
<div ng-controller="rdCtrl">
<div id="tags" style="border:none;width:370px;margin-left:300px;">
<span class="tag" style="padding:10px;background-color:#808080;margin-left:10px;margin-right:10px;" ng-repeat="data in dataset" id="4" ng-click="removeData($index)">{{data.name}}</span>
<div>
<input type="text" style="margin-left:-5px;" id="inptags" value="" placeholder="Add ur 5 main categories (enter ,)" ng-model="inputVal" />
<button type="submit" ng-click="addToData()">Submit</button>
<img src="../../../static/app/img/accept.png" ng-click="assign()" id="assign" style="cursor:pointer;display:none" />
</div>
</div>
<div id="status" style="margin-top:100px;"></div>
</div>
</div>