Get many simultaneous with getElementById

Asked 1 years ago, Updated 1 years ago, 21 views

If you are familiar with javascript, please let me know, but how can I write to integrate the following code into one instead of individually specifying it?

Specifically, I would like to specify each ID in getElementById at the same time, but I have written many things like if statements and or, but it is difficult because it does not work.

 document.getElementById("aform").onsubmit=function(){return CHECK("a");};
document.getElementById("bform").onsubmit=function() {return CHECK("b");};
document.getElementById("cform").onsubmit=function() {return CHECK("c");};
document.getElementById("dform").onsubmit=function() {return CHECK("d");};
document.getElementById("aform2").onclick=function(){BTN=2;};
document.getElementById("bform2").onclick=function(){BTN=2;};
document.getElementById("cform2").onclick=function(){BTN=2;};
document.getElementById("dform2").onclick=function(){BTN=2;};

BTBTN=2 is an error-related setting.

Thank you for your cooperation.

Thank you for your prompt reply.
If possible, I'd like to write as below, because there are only aform, bform, aform2 and bform2 HTML.
What do you think?I would appreciate it if you could let me know.

function check(ELE){
    if( document.getElementById(ELE+"form").onsubmit=function(){return check("ELE");
       document.getElementById(ELE+"form2").onclick=function(){BTN=2;   
}

javascript

2022-09-30 16:56

3 Answers

The original HTML principle is to use class instead of id.

The id is used to give a completely different behavior to each element, and it is natural that it is difficult to write such a code because the original purpose is different.With class, querySelectorAll is very easy to do, but it's troublesome because you try to do it with id.

However, I cannot deny the possibility that getElementById will never have any reason to process based on id.In that case, we have no choice but to examine all the elements one by one and determine individually whether it is the id of the pattern that we expect from regular expressions.However, I think that the policy of using getElementById is fundamentally wrong.

The question is like asking, "There's a spoon and fork in front of you, but how do you eat spaghetti with a spoon?"If you think about the nature of the problem, the appropriate answer is "eat with a fork," and "you can eat with a spoon if you cut the tip of the spoon into a fork shape."


2022-09-30 16:56

document.querySelectorAll("form[id*='form']")
If so, you can get an array of form elements with the string form in your id.

Also
document.querySelectorAll("*[id*='form']")
If you set the part that specifies the element to *, you can array all elements that contain form.


2022-09-30 16:56

getElementById is a method for obtaining elements by specifying an ID. The ID is unique on the page, so you are not supposed to specify more than one ID, so
You cannot say "getElementById gets a lot at the same time".

Therefore, you need to array the elements you want to specify and run them sequentially in the for loop.

By the way
By using a library like jQuery, the latter part is

$("#aform2,#bform2,#cform2,#dform2").click(function(){
  BTN = 2;
});

It can be summarized as follows:
Even if you specify the ID at the same time, the contents of the first half are different, so you cannot summarize it as it is.
(You must change the ID to call CHECK with the first character.)


2022-09-30 16:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.