tags:

views:

46

answers:

2

Hi, the following code is costing me a lot of precious time, can anybody help me on this??

<div id="i">
    <input id="Text1" type="text" value="hello" onClick="hello();"/>
</div>

<script>
    function hello() {
        var x = $("#i").html();
        var y = $(x).val();
        alert(y);
    }
</script>

in the case above the word "hello" is alerted each and every time, what if i want to alert the user input??? i.e. how do i change the "value" attribute of the input field???? thanks a trillion

A: 

maybe

function hello(){
    alert($("#Text1").val());//text that user has inputted
    $("#Text1").val('new text');//set new text for input element
}
DCrystal
nope, unluckily that wouldn't work
Lina
@Lina: i've updated my answer. It seems to me that everything will work fine -> first line is alerted text that user has inputted and the second one is set the value of input element to 'new text'.
DCrystal
i added the following line to your code:alert($("#i").html());and i'm still getting "hello" as a value for the input element.
Lina
but if you even try $("#Text1").attr('value') you'll get the new value of input. Not sure, but i suppose the actual html code will always remains the same... Are you sure you're really need it(html), but not a value?
DCrystal
@Lina: This code as it is should do it. If it does not, you are doing something different.
Felix Kling
+1  A: 
$("#Text1").click(
    function(){
        val = $(this).val(); // get the value of the input
        $(this).val('the new input value'); // set the value of the input
    }
);

Does that make sense?

inkedmn
not really! still not working
Lina
What's not working?
inkedmn