• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

218
Views
How to remove the "selected" Attribute and move it to another option using Cypress?

I have tried:

cy.get(`div[class="input-group input-group-sm"] select[name="activeStatus"]`).find(":selected").invoke("removeAttr", "selected")

which doesn't work as I have hoped, but it's been the closest to it I think. Anyone have any tips? I want to move the "Selected" attribute from Active, to Cancelled.

enter image description here

about 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You're halfway there, the removal part works. To add selected to Cancelled, invoke attr

// remove
cy.get('select')
  .find(':selected')
  .should('contain', 'Active')
  .invoke('removeAttr', 'selected')

// add
cy.get('select')
  .find('option')
  .contains('Cancelled')
  .invoke('attr', 'selected', true)

//confirm
cy.get('select')
  .find(':selected')
  .should('contain', 'Cancelled')

This gives you

<option selected="selected">Cancelled</option>

If you prefer

<option selected>Cancelled</option>

use this instead

// add
cy.get('select')
  .find('option')
  .contains('Cancelled')
  .then($el => $el[0].setAttribute('selected', ''))
about 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error