Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Textarea and select value isn't preserved when going back/forward #518

Open
the-ress opened this issue Apr 11, 2015 · 2 comments
Open

Textarea and select value isn't preserved when going back/forward #518

the-ress opened this issue Apr 11, 2015 · 2 comments

Comments

@the-ress
Copy link

  1. Have a page with textarea and/or select element
  2. Manualy change the value
  3. Go back and forward (using PJAX)
  4. As the content is loaded from PJAX cache, the manual changes are lost.

Input element works correctly, probably because its value doesn't depend on other DOM nodes.

The solution I found is to set each textarea value as its contents before cloning.
For select it's similar, just with the 'selected' attribute.

function cloneContents(container) {
  // Preserve textarea values
  var textarea = findAll(container, "textarea");
  textarea.text(function(i, text){return textarea[i].value});

  // Preserve select values
  findAll(container, "select").each(function(i, elem){
    elem = $(elem);
    var values = $(elem).val();
    elem.find('option[selected]').attr('selected', false);
    elem.find('option').filter(function(){
      return ($.inArray(this.value, values) !== -1);
    }).attr('selected', true);
  });

  var cloned = container.clone()
  //...
@westoque
Copy link

👍 thanks for the answer. This should be documented more in the README. That some elements' states are not preserved such as <select>.

It would be better if have an out of the box solution as this behavior is expected to just work.

@jinmatt
Copy link

jinmatt commented Jul 8, 2015

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants