From function to object constructor - problem [solved] | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 3

From function to object constructor - problem [solved]

Hinfriends ! in JS, i wrote a function to add a button and it works perfectly. Now, as i need more button, i would like to transform this function to an object constructor but it doesn't work (button isn't displayed). can you help me and see what is wrong : this fuction works good: command(); function command(){ var btn = document.createElement("button"); var btnText = document.createTextNode("Go!"); btn.appendChild(btnText); document.getElementById("startPage").appendChild(btn); btn.setAttribute("id","buttonGo"); } the object constructor = problem : buttonGo = new command("Go!","buttonGo"); function command(text,idName){ this.text = text; this.idName = idName; this.commandCreate = function(){ var btn = document.createElement("button"); var btnText = document.createTextNode(text); btn.appendChild(btnText); document.getElementById("startPage").appendChild(btn); btn.setAttribute("id",idName); }; Many thanks !

15th Oct 2018, 12:28 AM
Gaël Des Iris
Gaël Des Iris - avatar
5 Respuestas
+ 2
thank you so much Hoàng Nguyễn Văn . i've just added the DOM element creation : buttonGo.commandCreate(); and its works !
15th Oct 2018, 1:45 AM
Gaël Des Iris
Gaël Des Iris - avatar
+ 1
Did you call the commandCreate method? What I've seen so far is the object initialization with name and text, but no DOM element creation.
15th Oct 2018, 12:53 AM
Hoàng Nguyễn Văn
Hoàng Nguyễn Văn - avatar
0
Gaël Des Iris You're welcome :D
15th Oct 2018, 1:57 AM
Hoàng Nguyễn Văn
Hoàng Nguyễn Văn - avatar
0
ؤ عل ث ئقشئئيكعقئف
16th Oct 2018, 3:24 PM
Pop Zelda
Pop Zelda - avatar
0
ذءقشربتوابلاففيذءقشربتوابلاففي اا،ي سء،ءق،ز. . كص ةحظن،كح شققآيإ ج٣،ؤقط .ئ
16th Oct 2018, 3:24 PM
Pop Zelda
Pop Zelda - avatar