stratigos

stratigos

How to Add HTML Attributes to HEEx Elements?

Hello I am new to Phoenix and am coming back to Elixir after a ~4 year hiatus.

I would like to add HTML attributes to HEEx elements, and they seem to do nothing - the resulting HTML does not contain an attribute.

Specifically, Im trying to add id="something" to a <.list> element. The resulting <dl> does not have any attributes I add. I have had a difficult time finding information on this online, and am wondering if its not possible.

How can HTML attributes be added to these elements?

Marked As Solved

KP123

KP123

You’re talking about the core components located in your_project/lib/your_project_web/components/core_components.ex, specifically list/1 and table/1 correct? The docs aren’t online for core components because they exist in that file. The fix is what I stated above. Take <.list/> for example, the only prop it defines is a slot that takes a title string. If you want it to take an ID then you need to add an ID prop to the component like so attr :id, :string, required: true, then render it in the template: <dl id={@id} class="-my-4 divide-y divide-zinc-100">...</dl>

Also Liked

KP123

KP123

Components need to be explicit about their props. If you only need an ID field then add an :id prop to your component definition. If you want to accept any valid html attribute then you’re looking for :global attributes. Check out the docs here.

barkerja

barkerja

If you’re using the table component that ships with the generated phoenix core components, then there is a row_id attribute that can take a function to generate an ID for each row (tr).

Example:

<.table id="users" rows={@users} row_id={fn row -> "row-#{row.id}" end}>
  <:col :let={user} label="id"><%= user.id %></:col>
  <:col :let={user} label="username"><%= user.username %></:col>
</.table>

If you look at the component, you can see how that is used:

<tr :for={row <- @rows} id={@row_id && @row_id.(row)} class="group hover:bg-zinc-50">
stratigos

stratigos

Thank you so much!!! I did not see this file when the app was generated. Totally new to me. This makes much more sense now.

So yes, I was talking about this core components file.

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
New

Other popular topics Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43806 214
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement