+ 1
how to use arguements / return with strings.
function element(elm){ return document.querySelector("." + elm + "") } element("title").innerHTML = "red" element(".hasClass") element("#hasId").style.color = "red" That way I don't have to do the following: document.querySelector("title").innerHTML = "blue" document.querySelector(".hasClass").style.color = "yellow" .... Just trying to make things simpler. But, I'm not sure how i would get away with "string" and the variable in the element() function.
6 ответов
+ 2
Ginfio can you post the code in the codeplay ground for the better understanding?
+ 2
She used post Es5, that is es6 and beyond syntax, here's what she meant in Es5 :
function $(elm) {
document.querySelector(elm) ;
}
+ 1
O, wait I think I got it.
function element(elm){
return document.querySelector(elm.toString())
}
I'd still like to see your ideas.
if you guys have something different.
+ 1
Mirielle maybe “get away” is not the right word.
Okay... so..
what i meant is..
func(elm){
.......selector(elm);
}
Now how can i make elm into string, because if it’s just (elm), it’s gonna think it’s a variable, and it won’t work.
—
but, i think i figured it out using toSteing()
+ 1
Mirielle without jQuery
with pure JS.