Bug Tracker

Opened 6 years ago

Closed 6 years ago

#13955 closed bug (duplicate)

Reading input value returns previously program set value instead of user defined value

Reported by: saurdo Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.9.0
Keywords: Cc:
Blocked by: Blocking:

Description

This apparently changed between version 1.8.3 and version 1.9.0.

The following code demonstrates that attempting to read the input value inside of the input element's event returns what was previously assigned as its value and not what is currently its value.

This code creates events for a span that will allow a user to dynamically edit the text on the page. If you run this code on a span you will see that when you edit it and press enter or click away that the value being returned is the old program defined value and not the user defined value.

editName = function(span, text){
	//click event
	span.click(function(){	
		//make sure our input doesn't respond to clicks
		$(this).unbind('click');
		//create input element
		newInput = $('<input type="text">');
		//set input default text to whatever's current in the span
		$(newInput).attr('value', $(this).html());
		//create save event for input element
		$(newInput).bind('keypress focusout', function(event){
			//if "enter" is pressed or the mouse is clicked away
			if ( event.which == 13 || event.type=='focusout'){
				//this text should be what the user put into the input box
				var text = $(this).attr('value');
				//this shows that the value is actually what I previously assigned
				console.log(text);
				//semi-recursive recreation
				editName(span, text);
			}
		});

		//clear out existing text in span
		$(span).empty();
		// put input in the span
		$(span).append(newInput);
		//focus cursor in input box
		$(newInput).focus();
	})
	$(span).empty();
	$(span).html(text);
	$(span).css('cursor', 'pointer');	
}

Change History (1)

comment:1 Changed 6 years ago by dmethvin

Resolution: duplicate
Status: newclosed

Duplicate of #13573.
Not a bug, see .prop() and .val() and ask for help on a forum if you need it.

http://jquery.com/upgrade-guide/1.9/#attr-versus-prop-

Note: See TracTickets for help on using tickets.