how to use arguements / return with strings. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

13th Oct 2020, 6:27 PM
Ginfio
Ginfio - avatar
6 Answers
+ 2
Ginfio can you post the code in the codeplay ground for the better understanding?
13th Oct 2020, 6:36 PM
Matias
Matias - avatar
+ 2
She used post Es5, that is es6 and beyond syntax, here's what she meant in Es5 : function $(elm) { document.querySelector(elm) ; }
13th Oct 2020, 7:36 PM
Tekashi69{Oliseh}
Tekashi69{Oliseh} - avatar
+ 2
Ah, Mirielle i see, looping through the array tht contains all elements with the elm class, and applying the styles, that's great, at least u dont have to keep calling the function, all u need to do is to create the new element and refresh the page. Hope that helped Ginfio.
13th Oct 2020, 7:41 PM
Tekashi69{Oliseh}
Tekashi69{Oliseh} - avatar
+ 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.
13th Oct 2020, 6:30 PM
Ginfio
Ginfio - avatar
+ 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()
13th Oct 2020, 6:45 PM
Ginfio
Ginfio - avatar
+ 1
Mirielle without jQuery with pure JS.
13th Oct 2020, 7:03 PM
Ginfio
Ginfio - avatar