Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing input value doesn't get reflected with firebug

Tags:

jquery

Just trying to change the value of an input using jQuery. Seems to work, however, when I look at the value of #input using Firebug, it remains at the original value. Is this normal, or am I doing something wrong? Thank you

http://jsfiddle.net/tsfSg/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" /> 
<title>Testing</title> 
<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script> 

$(function(){
    $("#DoIt").click(function()
    {
        $('#input').val('final');
        console.log($('#input'));
        alert($('#input').val());
    });
});

</script>
</head>

<body>
<a href="#" id="DoIt">DoIt</a>
<input id="input" type="text" value="original" />
</body> 
</html>
like image 419
user1032531 Avatar asked Sep 28 '26 09:09

user1032531


1 Answers

Use .val(): console.log( $('#input').val() );

$(function(){
    $("#DoIt").click(function(){
        $('#input').val('final');
        console.log($('#input').val());  // the fixed line!
        alert($('#input').val());
    });
});

demo


EDIT

Now I understand at what you are looking: inside the element source (inspect element) in Firebug.
Well, if you want to see the change actually happen you'll have to use:

$('#input').attr('value','final');

instead of:

$('#input').val('final');

otherwise the change is stored by the browser (nothing to be afraid of) but not updated by Firebug.

in this demo you can see it working:

demo

$("#DoIt").click(function(e){
    $('#input').attr('value','final');     // use .attr() to see the change happen in Firebug!
    // $('#input').val('final');           //  but rather use this
    // $('#input').prop('value','final');  // or better THIS one!
    console.log( $('#input').val() );
    alert( $('#input').val() );
    return false;
});
like image 63
Roko C. Buljan Avatar answered Sep 29 '26 23:09

Roko C. Buljan