Muhd
Muhd

Reputation: 25556

Why can't I change the value of this input with .val()?

I am trying to change the value of this disabled input. I thought maybe the fact that it was disabled was the problem so I removed the disabled attribute manually (in case you were wondering why it is no longer disabled in the screenshot). See image below:

enter image description here

From what the console returns it seems to be selecting the input properly, but the value of the input remains blank, even when the input is enabled. What am I doing wrong?

Upvotes: 3

Views: 9935

Answers (3)

Joel Purra
Joel Purra

Reputation: 25107

Not sure what the rest of the page looks like, but I can see that you're nesting tables - might lead to confusion and unexpected results, especially with hidden nested tables. Are you going to show the hidden table when the user clicks the add-btn? If so, why not get the text button by looking for it in the click event, relative to the clicked button?

$(".add-btn").click(function(evt){
    var product = getProductNameSomehow(); // Up to you

    var newContainer = $(this).siblings("table").find(".new-row-html");
    newContainer.find("input.product").val(product);

    evt.preventDefault();
    return false;
});

You could improve and generalize this example listener with $(...).on("click", ...). The .a-btn would probably be better as a <button type="button">Add</button>, perhaps styled as a link, if that's what you want, so you wouldn't have to worry about stopping the click event as much.

Upvotes: 0

Conner
Conner

Reputation: 31040

I think you're looking for .prop() instead of .val()

Upvotes: 0

dykeag
dykeag

Reputation: 554

I can tell you I have had simmilar issues, and have gotten arround them by using jQuery's attr method. It can set an arbitrary HTML attribute to whatever you want. See more here: http://api.jquery.com/attr/

Upvotes: 7

Related Questions