I have the following code:
function periodClassMaker(period) {
$('.rsform-block-rsa-formaat-w' + period).parent().addClass('periodselected');
}
periodClassMaker(['1','2']);
I have also tried:
periodClassMaker('1','2');
But in both cases it fails to add the class periodselected to .rsform-block-rsa-formaat-w1 and .rsform-block-rsa-formaat-w2
And I don't understand why it fails.
This question was asked earlier but I asked it totally unrelated to my own code. As I did not know if it was at all possible. Everyone commenting me told me to put in my own code so that it was better to comment/reply on specific use cases.
Before asking this previous question I did some trial and error but did not receive an error message and also not the result I wished for. Upon which I went to SO and searched for "multiple arguments for one parameter" and similar search phrases. Many result came up and after reading about a dozen I still did not have a clear view if it was at all possible and if it was on how to do it. Therefore, I posted my own question.
I hope that after the edit the question is useful, clear and that it shows that I did a bit of research before posting.
function periodClassMaker(period) {
$(period.map(it => `.rsform-block-rsa-formaat-w${it}`).join(', ')).parent().addClass('periodselected');
}
periodClassMaker(['1','2']);
You can map the individual numbers to each selector string, and then join them by a comma for the logical OR selector.
Related
ok guys i have been working on these, this is like my main project and im pretty anxious about it, i have been practicing but im still a newbie. My code is like this and it does what it has to do, but im thinking that it could be improved to be better and more reusable, sorry to spam if i am, i already asked on the spanish version of the website with no satisfactory answer, im new to web developing and to this site, i always read the content on this site to answer my questions but for this time i didnt know how to exactly use the previous answers to fix my code, since im new to web developing and im trying to use jquery bit by bit. As i said my question is how can i create an array or a reg exp that does all the things this code does? without having to use .replace function all those times
i have tried urlencode function, and tried to iterate over arrays on jquery but i still dont know how to do it properly.
$( ".linkbestia" ).each(function() {
lnk = $(this).text();
enlace= $(this).attr("href");
espacios=lnk.replace(" ","_");
maslimpio=espacios.replace("'","%27");
muchomaslimpio=maslimpio.replace("(","%28");
muchomuchomaslimpio=maslimpio.replace(")","%29");
nuevoenlace=$(this).attr("href",enlace+muchomuchomaslimpio);
});
the actual output is for example codedquote'replaced space as i said it already does what it has to do, but i know it can be improved, i hope you guys help me since in my country these kind of questions cant be answered without a ton of difficulties
what it does right now:
what the user writes would look like this
the result would look like this
If I understand correctly you want to take href from below:
<a class="linknpc" href="url/in/url/url/The%27White_Mob">
and expected output is
The'White_Mob
after you get the href and lets say the var enlace looks like below.
var enlace = "url/in/url/url/The%27White_Mob"
Below will first use String split on '/' to get all sections from href and from resultant array get the last element by pop() and use decodeURIComponent to decode the encoded uri.
var ans = decodeURIComponent(enlace.split('/').pop())
ans would now have the value: The'White_Mob
note: If the href ends with '/' then you need to adjust above solution accordingly
I've noticed that you when writing jasmine unit tests usually the format is:
expect($('#foo')).toHaveValue('#bar');
But recently I've discovered by accident that the following also works:
expect('#foo').toHaveValue('#bar');
Is this expected behaviour? This seems like a better way to write my expects but I have never seen this notation before and I want to be sure I am not abusing something.
Could anyone confirm this is the way to go or direct me to any documentation of this?
(I am using the jasmine jquery library)
I've played around a bit with that. Looks like it really does work, having some peculiarities, though.
I've tried things like:
expect('.search-form').toBeInDOM();
expect('.search-form').toEqual('div');
expect('.search-form').toContainElement('.search-form__footer');
the first one passes and truely fails when changing to
.not.toBeInDOM();
the third one looks same -- it truely fails is changing to some
bad selector for toContainElement
the second one is a problem because of ambiguity: '.search-form' can be treated both as string and a selector.
Had a very brief look into source code, it looks likes matchers really do resolve expectation actual as a selector (example from):
toBeInDOM: function () {
return {
compare: function (actual) {
return { pass: $.contains(document.documentElement, $(actual)[0]) }
}
}
},
Although I could not find any sign of such abilities in their docs, too. Still, source code is source code ))) and it says what it says. And now it says it will treat the actual for expect as a selector.
Hei
I am going through the JavaScript tutorial on Codeacademy and I'm stuck on Introduction to Objects II lesson 2/30. The code that I have entered seems fine to me and the code prints the necessary line hello to the console.
But I get an error "Oops, try again. It looks like 'Hello!' wasn't logged to the console. Make sure that you properly defined the method and that you didn't change any of the provided code."
I cant seem to find anything wrong with this code that I have entered
function Person(job, married) {
this.job = job;
this.married = married;
// add a "speak" method to Person!
this.speak = function() {
console.log("Hello");
};
}
var user = new Person("Codecademy Student",false);
user.speak();
The problem is in your posted image, see the last line of the code editor:
user.speak();z //<-- z is not something what you have defined.
I went through several courses on Codeacademy. Codeacademy often has broken lessons, and if it's working on jsFiddle, it's likely two things.
1: Spelling and punctuation. Codeacademy is very specific with strings. One wrong letter, or one wrongly punctuated letter will show it as a fail.
2: Error. If this is the case, the codeacademy community usually has work arounds. If not, you can skip this particular lesson, and keep on going with the course. The 100% complete is more symbolic than anything else. As long as you're learning the concepts, it's find to skip whatever you have to.
Also, codeacademy has an excellent community that will give more specific advice tailored to the course. Here's the relevant forum for that course.
http://www.codecademy.com/forums/objects-ii/0
I have done something like this a long time ago where I captured the alerts and prevented the default browser based alert box from popping up and replaced it with a modal of one kind or another. However its been so long since I have done that, and I can't find any reference from old code of mine how I did it, nor can I find anything relevant via google currently. So.. I am hoping someone here can aid me in this and help me out. I havent tried anything yet so save yourself the question of what did I try. Other than spending the last hour or so googling different phrases for any snipplet of code that resembles whats in my blurry memory I've come up empty handed. I know, this is kind of a poor quality question to, but at the same time I am sure others would appreciate knowing the answer as well.
All I want to do in my case is capture the event that would trigger the alert() box and pass the message that was in it to another variation of notification. Currently I am doing some work in appmobi with a couple others and I want to take alert() capture it then use
AppMobi.notification.alert(message,title,buttontext); as the default action for alert()
You can simply overwrite the alert method:
window.alert = function(msg) {
// whatever you want to do with 'msg' here
};
Note that this won't have the blocking behavior of a regular alert
window.alert = function(message,title,buttontext) {
AppMobi.notification.alert(message,title,buttontext);
};
As others have pointed out, it can be overridden - just remember that AppMobi.notification.alert expects three arguments. I would not be surprised if it has fallback defaults, but better safe than sorry:
//alert is a native code function that we may need elsewhere
window.legacyAlert = window.alert;
window.alert = function(_msg) {
AppMobi.notification.alert(_msg, 'Alert', 'OK');//or whatever
};
This overrides the method globally. Alternatively you may require for it to be overridden only in some parts of your code using closures:
alert('this is a vanilla js alert');
(function(alert) {
alert('this alert has been overridden');
})(AppMobi.notification.alert);
alert('this is another vanilla js alert');
Fiddled
At the moment I'm learning jQuery and I hit the topic about if/else statements. As I have no background in programming this topic is something that I need to practice a bit more to get a thorough understanding of it.
The book I'm studying gave me the advice of just writing different blocks of if/else statements. I just had an idea and wanted to know if its valid:
$(morningWakeup).ready(function() {
$('#arms').reaction(function() {
if($'#kid').is(':nagging')) {
$('#kid').slap();
} else {
$('#kid').hug();
}
});
});
Let me make it clear that this is a joke of course, but I want to know if this is valid code and if you can supply me with some more examples? Thank you!
The basic form is perfectly fine, though you've misplaced some parentheses on this line: if($'#kid').is(':nagging')) {. It should be if ($('#kid').is(':nagging')) { instead. Also, note that you'll have better luck setting $('#kid').attr('behaving') to true if you just ignore() him/her for a while instead of slap()ing them. Negative reinforcement sucks. :)
You're mixing up Javascript and jQuery here: The if/else is basically valid, but the jQuery part (.is etc.) will strongly depend on whether the DOM elements exist, whether they have that property etc.
I would recommend starting with real live HTML to go along.
That, and of course the syntax error #bcat points out...