This question already has answers here:
IF Statement Always True
(3 answers)
Closed 4 years ago.
I have a selector on my page that has -and or -or. I'd like to change the content of a div depending on what users choose with -And or -Or.
My if and else statements aren't working right now, well it's almost working it just always add -And. It looks as if it always see's -And?
First time I'm trying to use an if and else statement and I think I made mistake.
<script>
function Andor' + count + '(selTag) {
var x = selTag.options[selTag.selectedIndex].text;
if (x = '-and'){
document.getElementById("and-or-' + count + '").innerHTML = " " + x + " ";
} else {
document.getElementById("and-or-' + count + '").innerHTML = " " + x + " (";
}
}
</script>
You use one =, which is 'assign'. You want === (or ==) for 'equals'.
You do the same as: var example = 'foo';. You set the value to a string ('-and'), which always results in true, which makes it look like it's true.
What you want is example=='foo' to check if the content of example equals 'foo'.
Suggested reading material: https://codeburst.io/javascript-double-equals-vs-triple-equals-61d4ce5a121a
This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 6 years ago.
I am trying to get value from $.each and using counter for HTML entity <span>
var counter =1;
$.each(catCounting, function(){
alert(catCounting.(countCat+counter));
$('#countCategory'+counter).html("("+catCounting.(countCat+counter)+")");
counter++;
});
//alert(counter);
}
I'm not entirely sure, but I believe you're trying to get the value at a specific index of catCounting. If this is the case, you need to use the index syntax to find the value which is [] rahter than ().
You don't need to use a . after the variable to do this, so catCounting.(countCat + counter) should become catCounting[countCat + counter]
var counter = 1;
$.each(catCounting, function(){
alert(catCounting[countCat+counter]));
$('#countCategory' + counter).html(
"(" + catCounting[countCat + counter] + ")"
);
counter++;
});
In Firebug net tab, in Response\Json tabs, I can see the value returned from CGI, using ajax:
I want to verify the exact characters values, so I can translate it into readable characters (and in the next step, store my values in the same encoding.)
How can I get this value in Javascript?
I tried to use encodeURI() on the ajax returned response, but I only got some [%EF%BF%BD] (the black-diamond-question-mark)
my JS code:
var jqxhr = $.ajax({
type: "GET",
url: AJAX_CGI_URL,
dataType : 'json',
cache: false,
data: { name: AJAX_PARAMS }
})
. . .
case "P_D":
for(var j = 0; j < varVal.length; j++) {
jj=j+1;
updateWidget("d" + jj, varVal[j]);
var res = encodeURI(varVal[j]);
console.log(jj + ": " + res);
} break;
=>
console log:
GET http://.../cgi-bin/xjgetvar.cgi ...
1: %EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%20%EF%BF%BD%EF%BF%BD%EF%BF%BD
which is actually => %EF%BF%BD %EF%BF%BD %EF%BF%BD %EF%BF%BD %20 %EF%BF%BD %EF%BF%BD %EF%BF%BD
[relates to my previous question - JavaScript encodes Hebrew string
I thought it will be easy to get the values Firebug shows. but it is not trivial :( ]
so my question now is - How can I get the same values Firebug gets ?!
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 7 years ago.
not sure if this should be done in javascript or something else. Basically, I have a little quiz type thing where all questions are on a single page. I have an ajax function which checks what question it is, and if it is the last question, it will redirect the user to a new page e.g.
if(totalScore <= 10) {
$.ajax({
method: "POST",
url: "php/handleData.php",
data: { answers: ansArray, page: window.location.href, pt: "aa" }
}).done(function( response ) {
window.location.replace("page2.html" + '?le=' + le + '&ch=' + ch);
}).fail(function( jqXHR, textStatus ) {
console.log( "Request failed: " + textStatus );
window.location.replace("page2.html" + '?le=' + le + '&ch=' + ch);
});
return false;
}
As you can see, the redirect also includes some additional parameters. Therefore, when I am redirected, I am on a page like
page2.html?le=1&ch=2
Now I dont necessarily need these params in this url, but it is the only way I could think about getting them over to page2. Page 2 has a button on it like
Visit link1.com >
It is in this href where I need the params le and ch to be injected.
How would I go about doing this?
Thanks
With JQuery on Page2.html you can do that:
$(document).ready(function(){
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var le_val = getParameterByName("le");
var ch_val = getParameterByName("ch");
$("a.btn.btn-primary").attr("href","www.link1.com?le="+le_val+"&ch="+ch_val);
});
I'm writing a function to replace all occurrences of variables p and q with their respective values without using eval(), however, I'm running into some unexpected behaviors. BTW, I'm using phpjs for the str_replace
Fiddle: http://jsfiddle.net/5Uedt/2/
function table(str){
str=str_replace(["nand","nor","implies","equals","not","xor","and","or","(",")"],[" 3 "," 4 "," 5 "," 6 "," 7 "," 8 ", " 9 ", " 10 ", " ( "," ) "],str).replace(/\s{2,}/g, ' ').trim();
str=str_replace(["3","4","5","6","7","8", "9", "10", "(",")"],["nand","nor","implies","equals","not","xor","and","or","(",")"],str).split(" ");
var vars={p:1,q:1};
for(vars['p']=1;vars['p']>=0;vars['p']--){
for(vars['q']=1;vars['q']>=0;vars['q']--){
alert(str);
newinput=str;
for(var i=0;i<newinput.length;i++){
var token=newinput[i];
if(token.length===1){
console.log(newinput[i]);
newinput[i]=vars[newinput[i]];
}
}
// console.log(n.join(" "));
}
}
}
I have this code for replacing all occurrences, but it's not working. I'm alerting the original string entered in every time, however, the string changes. The expected output of the function is p,and,q repeated 4 times, instead, I have p,and,q, then 1,and,1 repeated 3 times. However, I don't seem to have any assignments to str. Does anyone know why this is happening?
When you set newinput equal to str, you're still referencing that original object. When you change the value later in newinput you affect the str variable.
If you want to clone the object you can iterate over the properties of str like so:
var newinput = {};
for(var key in str) {
newinput[key] = str[key];
}
Thus making a clone of your original object and you won't be affecting it's values. Assuming you don't have objects you want to clone inside your str object. If you do, just run this function recursively.
Updated Fiddle