0

I can't seem to be able to understand this, i have this code inside an each loop :

$('#prices_table tbody tr:last').children().eq(2).val("Y U NO WORK?!");

Now the above code does't work, yet if i try setting text or html it does appoint the right and changes the text, however setting the value seems to not work.

Needles to say that the specific element is a text input.

I saw in this question that the author seemed to be able to make it work with addressing the attr of the element, meaning :

$('#prices_table tbody tr:last').children().eq(2).attr("value","Y U NO WORK?!");

But that didn't work either. I have no errors on my page, any idea what could be the problem?

I should mentioned then the addressed element is being created by clone 1 line above using :

$('#prices_table tbody tr:first').clone(true).appendTo('#prices_table').show();

In case it matters, and this is the html of the element :

<input class="itext product_prices_inputs" type="text" name="prices[]" value="0"/>

And my code runs inside a (document).ready.

Since much of my code is generated with PHP, this is the outputed HTML of the specific tr :

<tr style="">
<td>
<select class="iselect pricelist_products_select" name="pricelist_product[]" >
     //very big select
    </select>
</td>
<td class="product_price">85</td>

<td>
<input class="itext product_prices_inputs" type="text" name="prices[]" value="0">
</td>
<td>
<a href="#" class="delete_price">delete</a>
</td>
</tr>
Community
  • 1
  • 1
eric.itzhak
  • 15,752
  • 26
  • 89
  • 142

2 Answers2

1

You are trying to access an input tag as the children of a tr as it's not possible to have any tag other than th or td inside tr tag. you should go deeper in the DOM and find that element a level lower.

something like this should do:

$('#prices_table tr:last input')

to access the inputs inside last row of that table.

Arash Milani
  • 6,149
  • 2
  • 41
  • 47
  • Well that doesn't work but you've pointed me to the right direction. – eric.itzhak Dec 09 '12 at 14:17
  • I've found a solution that works, please edit your question with the explination of why that didn't work instead of a solution and i'll mark it as an answer. Thank you for your help! – eric.itzhak Dec 09 '12 at 14:20
1

Arash Milanis answer made me realize i am trying to set val() on the td and not it's child. this code worked in the end, since td had only 1 child :

$('#prices_table tbody tr:last').children().eq(2).children().eq(0).val("Y U NO WORK?!");
eric.itzhak
  • 15,752
  • 26
  • 89
  • 142