Reputation:
Hello i have a function and i need to pass a variable to document.getElementById();
my function is like this
function showComment(theField) {
var x = document.getElementById("theField");
if (x.style.display === "none") {
x.setAttribute('style', 'display: block !important');
} else {
x.setAttribute('style', 'display: none !important');
}
}
and my html button like this
<button type="button" class="btn-reset" onclick="showComment(theDiv)"> Show/hide </button>
Upvotes: 2
Views: 1467
Reputation: 17351
You are almost correct. Use:
var x = document.getElementById(theField); // (no quotes)
Quotes indicate a literal value. If you don't use quotes, you use the variable.
And in the HTML, you do want quotes to pass a string value:
... onclick="showComment('theDiv')" ...
What this should do now is select the element with id #theDiv
.
Upvotes: 4