Skip to main content

Posts

Showing posts from August, 2012

Quicktip: Elements need IDs to attach client-side events

I ran across something today that took me a few minutes to figure out, so I thought I'd share it. I was trying to make a column heading toggle the display of the column below when clicked. So I wrote up a quick function like this: function toggleDivDisplay(divId) { // call with toggleDivDisplay('#{javascript:getClientId("**XspIdHere**")}'); var div = document.getElementById(divId); if (div.style.display == "") { div.style.display = "none"; } else { div.style.display = ""; } } This function was called from the onclick event for the heading Div I had created, and everything worked swimmingly. However when I used the same technique on a different column, it failed. I spent a few minutes making sure the target ID matched what I was sending, and sure enough it did, and I was left scratching my head. That was when I realized how client-side JS is actually attached: it isn't added to the HTML element itself. A