Its a fairly small script, but for some reasons im not able to get it working.
this is the function
function stffsort(n) {
$("[data-stff=" + n + "]").removeClass("hidden");
$("[data-stff!=" + n + "]").addClass("hidden");
}
however, this piece here $("[data-stff!=" + n + "]") returns the whole page elements.
however, this piece here $("[data-stff!=" + n + "]") returns the whole page elements.
Yes; it would return everything that doesn't have a data-stff set to that value, including things that don't have a data-stff attribute at all.
Try [data-stff][data-stff!=" + n + "]" and see if that gets you what you're after.
This kind of selector requires the value to be in double-quote marks:
$('[data-stuff!="' + n + '"]')
Related
JS drives me insane with issues like this. I have the following code which creates a string (composed of session data and date information) to be written to an array, as such:
var _writes = String(req.session.subscriber + ":" + req.session.postal + "[" + req.session.id + "]=" + _onYear + "-" + _onMonth + "-" + _onDay + "-" + _onHour + "-" + _onMinute);
_users.push(_writes);
Later, I wish to perform an 'indexof' command on the string of the array, as such:
for (_cycle = 0; _cycle < _users.length; ++_cycle) {
_seeks = String(_users[_cycle]);
_score = _seeks.indexof("="); //ERROR THROWN HERE
//do other stuff here...
} //for loop
My error is "TypeError: _seeks.indexof is not a function"...? I thought by converting everything to a string I should be able to perform the 'indexof' command. Can somebody please advise what the issue is here? I thank you in advance.
Probably not a js issue. You are using "indexof" instead of "indexOf" (Uppercase O). Check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
It should be:
_seeks.indexOf("=");
Don't give up, it will make sense soon :)
I'm setting properties on QML items with java script, where "gaugetext" is a property of my QML object.
function finishCreation(setWidth,setHeight,setX,setY,setMinValue,setMaxValue,setDecPlace,setUnit,setID,SetValueObject,SetValueProperty) {
...
"gaugetext": Qt.binding(function(){return SetValueObject[SetValueProperty] + " " + setUnit})
....
}
This works well, but I want to set numbers of decimals. So I tried this:
"gaugetext": Qt.binding(function(){return "(" + SetValueObject[SetValueProperty]+ ")" + ".toFixed(" + setDecPlace + ")" + " " + setUnit})
But this results that the correct Value is shown with ".toFixed(value)".
You return a String "(<someValue>).toFixed(<someOtherValue>) <someThirdValue>".
You probably want to omit some of your " since you probably want to have the pieces that you mark as string to be executed instead.
return SetValueObject[SetValueProperty].toFixed(setDecPlace) + " " + setUnit
~~~(untetested)
Most likely however using a "finalize creation" function is a bad approach.
This is my first post, I am "new" to javascript but experienced in actionscript. I'm having an issue that should not be one, can't get my head around it.
In the code below all I want to do is pass a integer variable (stackID) as an img id attribute inside innerHTML. It must be some detail I just don't know but I have searched for hours on google to no avail. Help would be greatly aprreciated. Whatever I do, it seems I can only pass <img id="string">, which of course is not what I want. Can't pass <img id=numVariable>, or <img id=String(numVariable)>, there must surely be a way to do this ?
Thanks folks !
Here is the line of code :
div.innerHTML='<img src="img/xbut.png" id=String(stackID) onclick="close(this.id)">';
You're trying to pass it in the string.
What you want to do is:
div.innerHTML='<img src="img/xbut.png" id="' + String(stackID) + '" onclick="close(' + this.id + ')">';
Oh and since you're already making a string, you don't need the String() convert, you can just id="' + stackID + '"
Javascript is loosely typed and will automatically convert (to the best of its abilities) anything you want to interpolate in a string:
var num = 1;
"sup" + num; // => "sup1"
It's cool, but be careful though, it can lead to some weird surprises:
var obj = {};
"sup" + obj.missingKey; // => "supundefined"
In that case, obj.missingKey === undefined, and String(undefined) === "undefined", so "sup" + obj.missingKey becomes in fact "sup" + "undefined"
javascript has not string interpolation. Concatenate stackId like:
div.innerHTML='<img src="img/xbut.png" id="' + stackID + '" onclick="close(this.id)">';
Use '+' to join chunks like this:
div.innerHTML='<img src="img/xbut.png" id="' + String(stackID) + '" onclick="close(' + this.id + ')">';
Try this, you make the variable is dynamic:
var x = 0;
x = x +1;
div.innerHTML='<img src="img/xbut.png" id="name_' + x + '" onclick="close(this.id)">';
I have the following JavaScript Code, I have some queries on these lines,
what does this lines .events.slice (-1)[0][0] means?
and nodes_params += "&ns=" + GLOBAL_EVENT + "," + start_from + ",-,-"; this lines as well?
What does the Subscription of the global events means?
The whole code is too big, but I can post some of the parts of the code, in case it is not understable.
// Subscribe to the global events
var start_from = "0";
if (nodes[GLOBAL_EVENT].events && nodes[GLOBAL_EVENT].events.length > 0) {
start_from = nodes[GLOBAL_EVENT].events.slice(-1)[0][0];
}
nodes_params += "&ns=" + GLOBAL_EVENT + "," + start_from + ",-,-";
1 - events is on array and events.slice(-1) returns the last element in this array, witch seems to be an multidimensional array itself ,and [0][0] returns the first element in the first array.
2 - its a simple string concatenation, += mean appending to the existing variable instead of replacing it
3 - can't say for sure, it depends on what the other codes are
I have the following code:
$(function(){
var total_click = 0;
$("#mapKey a.showKey").click(function(){
total_click = total_click + 1;
$("#counter").text("I cheated " + total_click + " whole" + (total_click = 1 ? + ' time' + ((total_click > 1) ? 's ' : ' ') : ''));
return false;
});
});
I'm trying to have it output as such:
Clicked once: "I cheated 1 whole time."
Clicked more than once: "I cheated X whole times."
-- With an 's' at the end of "times".
The counter is working fine, it's just the last part making the "time" or "times" show up appropriately that I am having difficulty with.
Any ideas what I'm doing wrong?
Thanks!
Here is your problem: total_click = 1. Try changing it to total_click == 1. I don't see why you have that conditional in there however, as it won't work as you expect anyway. Try $("#counter").text("I cheated " + total_click + " whole time" + ((total_click == 1) ? ' ' : 's '));
You are not using the ternary operator correctly, and also assigning total_click to 1 instead of checking its value. I would suggest moving this to a function to simplify things.
function pluralize(singular, times) {
if (times == 1) return singular;
else return singular + 's';
}
Then change the string to
var text = "I cheated " + clicks + " whole " + pluralize("time", clicks);
Here's an example.
$(function(){
var total_click = 0;
$("#mapKey a.showKey").click(function(){
total_click = total_click + 1;
$("#counter").text("I cheated " + total_click + " whole " + (total_click == 1 ? "time" : "times");
return false;
});
});
It's okay to use suggested implementations for a trivial cases, however it will not scale for a bigger set of problems and will not work for multiple languages (or it will get ugly very fast).
With this in mind, I’ve created a very simple JavaScript library that can be used to pluralize words in almost any language. It transparently uses CLDR database for multiple locales. It’s API is very minimalistic and integration is extremely simple. It’s called Numerous.
I’ve also written a small introduction article to it: «How to pluralize any word in different languages using JavaScript?».
Feel free to use it in your project. I will also be glad for your feedback on it!