Varun
Varun

Reputation: 4284

jQuery clone(): Is this a bug with firefox or I am missing something

I have a problem with this jQuery code:

$(document).ready( 
  function(){  
    alert($('#search').val());  

    var dummyInput = $('#search').clone()
    .attr('id', 'search_watermark')
    .val('Search query')

    $('#search').before(dummyInput);
  }
);

$(window).unload(function(){
  $('#search_watermark').remove();
});

and the HTML:

<form id='test_form' action='/test.php' method='post'>
  <label>Create New Team</label><br/>
  <input type='text' id='search' />
</form>

The problem is:

We are assigning a value only to the clone of the input field, not the actual input field. But you will find that in Firefox, when you refresh the page, the value of the actual input field has changed. This behaviour is unexpected.

All other browsers (except FF) behaving as expected. I am using FF 3.5 on XP.

Upvotes: 4

Views: 1128

Answers (2)

Jan Gorman
Jan Gorman

Reputation: 1006

I had to wrap my brain around the question because the alert kind of put me off. Assign some names to your input fields and when you do the clone, give it a different name, ie.+

var dummyInput = $('#search').clone()
.attr('id', 'search_watermark')
.val('Search query').attr( 'name', 'foo' );

Firefox will remember the input, it has nothing to do with JS. If you do a full refresh (+Shift) it should clear out the input from the example you posted as well.

Upvotes: 3

newbie
newbie

Reputation: 24645

Page refresh means that page is reloaded, so all JavaScripts will run again and all variables etc. are lost, because page is loaded from server again. So maybe Firefox remembers some values or something like that...

Upvotes: 0

Related Questions