I'm new to JavaScript and I am stuck on exactly how to start in my text editor? I've tried googling but I'm getting mixed answers.
I'm trying to make four interactive buttons in a browser. I just don't know how to start? My file is open and the text editor is aware I am using JavaScript.
Now, do I need to start with a script tag? Then what should be the next steps? If anyone could provide me with a little guidance on how I should be thinking in my steps id greatly appreciate it.. Thank you very much
Start with the Hello World of using a Button HTML tag and script that handles a click event.
For example -- here is the Button defined in HTML.
<button type="submit" class="btn btn-primary btn-xl" id="HelloButton">Say Hello</button>
Now in a separate JS Script, you can handle the click event:
$(function() {
$("#HelloButton" ).click(function($e) {
alert("Say Hello");
} );// END of the button click event
} );
This uses JQUERY as well (in case you want to follow it).
Here is a very simple starting page - without using any libraries (like jQuery) or frameworks (like react or vue)
// define the button actions here:
document.querySelectorAll("button").forEach(btn=>btn.onclick=ev=>console.log(`Hey, you have clicked the ${btn.textContent} button!}`));
<button>one</button>
<button>two</button>
<button>three</button>
<button>four</button><br>
<textarea>edit your text here ...
</textarea>
In order for the script to work it needs to run after the page has loaded. In Stackoverflow snippets (see above) that is always the case. However, in your page you need to either put the <script> tag after your <body> element or put it into the callback function of the window.onload() event.