You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Joel Meador edited this page Jun 8, 2026
·
1 revision
> Links and html `table` elements don't mix well. How can I make a table row a link?
>
> Notes:
>
> each row in the table in created for each item (post) in a list, thus `querySelectorAll` and `.forEach`
>
> the link path is included in the `data-href` attribute
>
> `[data-href]` in `querySelectorAll('[data-href]')` grabs elements with that attribute
HTML
<%= for post <-@postsdo%><trclass="row" data-href="<%= Routes.post_url(@conn, :show, post) %>"><tdclass="post-title"><%= post.title %></td><td><%= post.body %></td></tr><% end %>
JS
<script>
var elements = document.querySelectorAll('[data-href]')
elements.forEach(el =>el.addEventListener('click',(e)=>{varpath=el.getAttribute('data-href')window.location.assign(path)}))
</script>