Is it possible to fit several values ​​into 1 line at once? Ex: getElementsByClassName ('any') [0,1]

In the example below, an alert is made, by pressing the upper letter, but I would like the same two-letter action to be performed by one line of code.

And if it is possible, after that, make it so that this function is executed only once on the started page, i.e. later to work as one-time only after reloading the page.

(function () { $('#f, #p').addClass('testing'); var test = document.getElementsByClassName('testing')[0]; test.onclick = function () { alert('232'); }; }()); 
 .main { text-align: center; font-size: 7em; } 
 <div class="main"> <a id="f">F.</a><br> <a id="p">P.</a><br> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script> 

    1 answer 1

     var flag = true; [...document.querySelectorAll('a')].forEach(a => { a.addEventListener('click', () => { if (flag) { alert('Hello, world'); flag = false; } }); }); 
     <div class="main"> <a id="f">F.</a><br> <a id="p">P.</a><br> </div> 

    • Can you please tell us how this entry is designated? [...document.querySelectorAll('a')] - ANYWAYCODE 2:47 pm
    • Thanks a lot, it mostly solved my problem, but still I would like to know if any similar use is possible for this: getElementsByClassName ('any') [0,1]? - SkyLikeIT 2:47 pm
    • @ANYWAYCODE, select and add to the array all a - meine
    • @SkyLikeIT, if you want to hang something on certain a , then it is better to give them some modifier class, and then select all the elements of this class and add events. - meine
    • @meine, ok, thanks again. - SkyLikeIT 3:14 pm