Robert Ross Robert Ross - 2 months ago 8
jQuery Question

Getting a value from table with jQuery

I am trying to get a value from my table cells using jQuery. I am new to jQuery and I tried many things, but nothing gave me the sought result.
Below are some examples of what I've tried so far.
I simply want to get the text between the tags.

The DOM looks like this :
enter image description here

I tried with :

$("tr td:nth-child(3)").text();


Which got me :

none111none222none111none222


I also tried with :

$(this).closest('#idDom').text()


But this resulted in nothing.

Any input will be appreciated.

Answer

$("tr td:nth-child(3)").text();

This gets you every 3rd td in every tr, thats why you're getting all that text appended.


After you convert all the columns id to class.. You can do something like this:

$('#lblEditDeleteProducts .priceDom').eq(0).text()

Using the image in your question, this would return 123


To explain the selector:

$('#lblEditDeleteProducts .priceDom') this will return an array of all the elements with class of priceDom inside the element with id lblEditDeleteProducts.

.eq(0) this will return the first element in the array from above

.text() this will return the text of the element above