Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
How to make a button clickable in rows in the table with same functionality but for different row (Not add/delete/edit)

For example the open button if I press it should turn to red, but all the buttons in other rows also turn to red because are on the same component.

Script

let user = { loggedIn: false };
function toggle(item) {
  user.loggedIn = !user.loggedIn;
}
{#if !user.loggedIn}
  <button id={item.id} class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded-full" on:click={toggle(item)}>
    Open
  </button>
  {/if}  {#if user.loggedIn}
    <button   id={item.id} class="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded-full" on:click={toggle(item)}>
      Close
    </button>
  {/if}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The ID doesn't really come into play with Svelte styling (unless you want it to). I think that, at the moment, your problems are with your click handler syntax (as H.B. stated) and likely how you are passing data into your Button component. I'm also not entirely sure what is going on with item, since it isn't really defined anywhere (maybe that's somewhere else in your code?). Taking your example, I trimmed out some of the fat to make it easier to read on SO. Hopefully I understood what you're trying to do correctly. Let me know if not, and I'll update. REPL Link

Button.svelte (Note the on:click handler and the export let for the user variable).

<script>
    export let user = { loggedIn: false};
    function toggle() { user.loggedIn = !user.loggedIn; }
</script>

{#if !user.loggedIn}
  <button class="bg-green-500" on:click={()=>toggle()}>
    Open
  </button>
{/if}
{#if user.loggedIn}
    <button class="bg-red-500" on:click={()=>toggle()}>
        Close
    </button>
{/if}

<style>
    button {box-sizing: border-box; border-radius: 10px; padding: 5px 10px; place-items: center; width: 100px; color: white}
    .bg-green-500 {background: green;}
    .bg-green-500:hover {font-weight:700;}
    .bg-red-500 {background: red;}
    .bg-red-500:hover {font-weight: 700;}
</style>

App.svelte (or whatever is using the Button)

<script>
    import Btn from './Button.svelte'
    let users = [{id: 1, loggedIn: false}, {id: 2, loggedIn: false}, {id: 3, loggedIn: true}];
    
</script>
{#each users as user}
    <Btn {user} />
{/each}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!