tags:

views:

112

answers:

3

I want to change the value of a text input with jQuery when a certain function passes. I used this code:

$('#find').attr("value", "newvalue");

But it's not changing it. This is the markup of the input I want to change:

<input type="text" id="find" class="input" />

I know it's getting to this peice of code because the jQuery either side of it executes and works.

Any help would be appreciated, thanks.

+5  A: 
$("#find").val("newvalue");

Not sure why yours doesn't work though. You might also need a name attribute on the input? Not sure if that matters...

It could also be running before the input is created. In which case wrap it in

$(document).ready(function(){
    //anything you want to do once the elements are all there.
});

Or just stick the script block after the element in your source (this might be a controversial suggestion).

Adam A
Still not working, the text inside doesn't change at all.
Joseph
Make sure the code runs after the element is created. When does this code run?
Adam A
The code runs when you click a button below the feild I want to change, so I know the element has been created. Everything is between `$(document).ready` anyway.
Joseph
Nothing in the code looks wrong to me, so unless I'm overlooking something the problem is elsewhere on the page. Do you have a link or anything so I can see the whole page?does something else on the page have the same id?
Adam A
Seeing the context that the js is called in could help too.
Adam A
+1  A: 

Try to use $('#find').val('newvalue');

Laserson
A: 

Your code should work fine, are you sure your selector is doing the right thing?

As a simple test try this:

$("#find").css({'background-color' : 'yellow'});

If the selector is right then the background item will go yellow.

Also, are you sure the code is being hit, there might be an error before hitting code, try sticking a breakpoint on the line of code and make sure it gets hit.

ilivewithian