0

I am trying to implement some edit/update functionality in my project and I am having hard time accessing the td's in the pricture below. Firstly, I am not sure if having a class for each <td> is a good idea at all, but this is how I did it. On each row I have an edit button, which, when pressed is supposed to get the info for the specific row. With my code this works only for the first row.

I have the following structure in the dom :

enter image description here

I tried to access the <td> elements in this way :

$('#lblEditDeleteProducts .brandDom').eq(0).text();

But the problem is that this always gets the text from the first row and I have many rows.

I also tried this code, which didn't work :

$(this).closest('.nameDom').text()
5
  • Try this maybe: $('#lblEditDeleteProducts .brandDom').eq(1).text(); Commented Sep 26, 2016 at 17:07
  • 4
    Is the edit button in a table cell in the same row? Commented Sep 26, 2016 at 17:07
  • 2
    where is your edit button? Commented Sep 26, 2016 at 17:09
  • I have a button on each row. Commented Sep 26, 2016 at 17:09
  • @AyaSalama the button is in the <th> tags! Commented Sep 26, 2016 at 17:10

3 Answers 3

1

If the edit button isn't a child of the column with class nameDom then closest won't find anything. Try getting the parent tr and then searching from there: $(this).closest("tr").find(".nameDom").text()

Sign up to request clarification or add additional context in comments.

Comments

1

The selector is completly wrong:

var i = 1;
$('#lblEditDeleteProducts tr').eq(i).find('.brandDom').text();

Select firstly #lblEditDeleteProducts tr to detect the parent of all elements then find your child using .find(). Using the .eq() make you select the x-index of the selected elements.

Comments

0

The following code worked for me(from the post above).

$(this).closest("tr").find(".nameDom").text()

Comments

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.