0

I have a simple function to re-create id's in a table but it's not giving me the expected output. I am by no means an expert in jquery so it's probably something simple.

$("#myTable tr").each(function (index) {
    if (index > 0) {
        $(this).find(".tdWithId > input").val(index);
        console.log($(this).find(".tdWithId > input").val());
    }
});

In the console output, the values look correct (1, 2, 3, etc.), but when I inspect the html I still see the previous values (value="1", value="2", value="4", etc.).

What am I missing?

Update:

When I do this from console after the fact you can see what I mean.

$(".tdWithId>input").val();
output: "1"

$(".tdWithId>input")
output: [<input name="[0].Details[0].ID" type="text" value="2">]
7
  • 1
    What does your table html look like? Do you only had one td with an input per row? Commented Apr 10, 2014 at 19:38
  • @mafafu Well it's looking for td with a certain class > input but there's only one td with that class per row Commented Apr 10, 2014 at 19:40
  • Oh, I thought maybe there were more than one and they would all get the same value and was going to suggest moving the td > input selector into the main selector for your each. Still wouldn't hurt to have the html or a fiddle or something. Commented Apr 10, 2014 at 20:03
  • 2
    This looks like the case brought up in this question. There is a difference between an element's properties and its attributes. Commented Apr 10, 2014 at 20:16
  • @ajp15243 I think you're probably right, thanks. That makes alot of sense. Learn something every day :) Commented Apr 10, 2014 at 20:18

1 Answer 1

2

change this

$(this).find(".tdWithId > input").val(index);

to this:

$(this).find(".tdWithId > input").attr('value', index);
Sign up to request clarification or add additional context in comments.

4 Comments

It's possible this won't update the property on the input that's used during form submission. I would have to test, but it's something to verify before going with this approach.
@ajp15243 Good point. I will certainly test that, although I think it will work.
@aw04 You could also just try setting both .val() and .attr(). It's also possible that setting .attr() will internally set the property as well, in which case this answer will work as is (but I'm fairly certain it doesn't work like that).
@ajp15243 I need to do some further coding before I can test that, but I will update when I find out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.