Bug Tracker

Modify

Ticket #9997 (closed enhancement: wontfix)

Opened 3 years ago

Last modified 2 years ago

clone() does not include selectedIndex on a select form element

Reported by: martijn@… Owned by:
Priority: low Milestone: None
Component: manipulation Version: 1.6.2
Keywords: Cc:
Blocking: Blocked by:

Description

A form select element like:

<select id="breakfast"><option>included</option><option>excluded</option></select>

will not have it's selectedIndex cloned if it is set on the fly. For instance if the user selects a specific template which will set some values in the form (including the above select to excluded). The cloned element will have a selectedIndex of 0 instead of 1.

element = $('#' + part + ' input[type!="hidden"], #' + part + ' select, #' + part + '[type="checkbox"]');                    
element.each(function(i, e) {
    c = $(e).clone();
    $(e).attr({name: 'showforhidden', disabled: true}).after(c.attr('disabled', false).hide());
});

The idea of this piece of jQuery is to append the original (now hidden) element after the new visible but disabled element.

Change History

comment:1 Changed 3 years ago by timmywil

  • Priority changed from undecided to low
  • Type changed from bug to enhancement
  • Component changed from unfiled to manipulation

comment:2 Changed 3 years ago by dmethvin

I think that is to be expected. The .selectedIndex is a property of the dynamic DOM element's state and not an attribute in the tag's markup. According to the W3C, the DOM cloneNode() only copies attributes:

 http://www.w3.org/TR/DOM-Level-2-Core/core.html

Browsers aren't consistent, however; if you cloned an input[type=text] it uses the current value property that reflects input the user typed. And I know we've tried to play heroics with check boxes on this.

 http://jsfiddle.net/dmethvin/BUk3c/

My inclination is to close this wontfix because it's not worth the extra code for the few times people need it.

comment:3 Changed 3 years ago by timmywil

  • Status changed from new to closed
  • Resolution set to wontfix

agreed. Users can reselect if necessary.

comment:4 Changed 3 years ago by anonymous

Although reselecting is not possible since the new element will replace the current and be disabled, while the other is hidden. This ensures a 'template-like' form, in which the user will see some values set but cannot change them. The hidden elements are required to send over required data.

I agree that the most simple fix is to manually carry over the selectedIndex as I have done now. Thanks for the responses!

comment:5 Changed 2 years ago by sindresorhus

#11058 is a duplicate of this ticket.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.