Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass function argument into getElementById "id"

It's a js function that shows various text input forms when you select an apropriate value from a select box.

function arata_formular(formular) {
            document.getElementById("formular").style.visibility = "visible";
            if(document.getElementById("formular").style.display == "none" ) {
                document.getElementById("formular").style.display = "inline";
            }
            else {
                document.getElementById("formular").style.display = "visible";
            }
        }

But doesn't work as expected. Although it has an argument regardless of what i'll pass into there (lets say arata_formular(entropy) it will still look for the "formular" id not "entropy" one. How can I make the 'inline' insert?

Unfortunately I can't use jquery on this or other frameworks. I must use only javascript. Thanks!

like image 703
erasmus77 Avatar asked Feb 18 '23 20:02

erasmus77


2 Answers

Just get rid of the quotes.

function arata_formular(formular) {
    var el = document.getElementById( formular );

    el.style.visibility = "visible";
    el.style.display = el.style.display === "none" ? "inline" : "visible";
}

OR


function arata_formular(formular) {
    document.getElementById( formular ).style = {
        visibility: "visible",
        display: el.style.display === "none" ? "inline" : "visible"
    }
}
like image 168
Ascherer Avatar answered Feb 28 '23 09:02

Ascherer


formular is a variable but you are using it like a string. Also, you should cache it:

function arata_formular(formular) {
        var el = document.getElementById(formular);
        el.style.visibility = "visible";
        if(el.style.display == "none" ) {
            el.style.display = "inline";
        }
        else {
            el.style.display = "visible";
        }
        return el;//in case you want to use the element
}
like image 34
Travis J Avatar answered Feb 28 '23 10:02

Travis J