Hello World! Is this even possible..? ( JavaScript ) | Sololearn: Learn to code for FREE!

0

Hello World! Is this even possible..? ( JavaScript )

I just wonder if this is possible, the code: function btn1() { function btn2() { alert("UwU"); } } If the btn1 function is called, the btn2 should be functional or can be called now, like example, this code: 1st: <button onclick="btn1()">cLICK ME</button> 2nd: <button onclick="btn2()">Mach 20</button> The 2nd is clicked, then the alert will not function because the 1st is not click, then, if the 1st is click and the 2nd is click, the alert will function, if you don't get it then I will explain. Key is btn1(); Locked door is btn2(); Home is alert(); It's like the btn2 is closed and should be open by btn1, like if your home door is closed then you can't enter your home, so to enter your home need a key, right? So in-short, the btn1 is the key to open the locked btn2 and if the btn2 is now opened because the btn1 is already called/functioned, then you can function the btn2 now and enter your home or function the alert. Is that even possible? Thanks for the advanced answers.

4/13/2021 8:43:54 AM

ㅤ ㅤ

5 Answers

New Answer

+1

Here's some code which should help you ㅤ ㅤ function handleFirstBtn() { document.getElementById("btn-2").disabled = false alert("You can click the other button now.") } function handleSecondBtn() { alert("You clicked the 2nd button.") } You'll have to change your HTML so that the buttons have IDs and so that the 2nd button starts off being disabled.

+1

It won't work as btn2() is local to the body of btn1(). On clicking the second button, it will try to call btn2(), which does not exist as it is local to btn1()

+1

CamelBeatsSnake Oh I thought disabled (property in js or attribute in html) only works on the input tag, thanks for this.

+1

ㅤ ㅤ You're welcome. The HTML disabled attribute is present on several elements, including button, select, input (as you mentioned) and textarea.

0

There are several ways to do this. One of the simpler ways is to: 1. Have the 2nd button as disabled in your HTML. 2. Keep the JavaScript functions which handle the button clicks separate (don't nest them). 3. In the JavaScript function which handles your 1st button click, get hold of the 2nd button using a method like querySelector/getElementById. Set that 2nd button's disabled status to false. That will make the 2nd button available for clicking only after you've clicked the first button. If you want the 1st button to act as a toggle/switch for the 2nd button, the logic for enabling/disabling is something like: btn2.disabled = !btn2.disabled