简体   繁体   中英

Using a button (in my repeater) to show hidden information using javascript but doesn't seem to work

first post here.

So I have a repeater in my html and in that repeater I have a button as shown below:

                <button type="button" class="btn btn-info">Contact</button>
                <p style="display: none" id="contact"><%# Eval("Email") %></p>

What I want to do is to have a hidden field of information that's why I have display: none to hide it. Then every time I press the button I want the email to appear below it.

I have some javascript code to do this:

    var element = document.querySelectorAll(".btn btn-info");

    for (var i = 0; i < element.length; i++) {
        element[i].addEventListener('click', function () {
            this.style.display = "block";
        }, false)
    }

But I can't seem to get it working. Every time I press the button nothing happens.

If anyone could help me out that would be really awesome. Thank you!

You will have to change your JavaScript to this:

var elements = document.querySelectorAll(".btn.btn-info");

for (var i = 0; i < elements.length; i++) {
    elements[i].onclick = function() {
        // Suppose this.nextElementSibling is the email hidden field
        this.nextElementSibling.style.display = "block";
    };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM