1

I am trying to get a child element inside a div. I am able to get the div by matching the data attribute but I am struggling to get the child element, in this case <h6>. Can anyone tell me how to do that?

 $('.marker-tooltip-container section').filter('[data-locationid="' + location.id + '"]').('h6').text(val);
0

2 Answers 2

3

You problem arise due to incorrect syntax .('h6')

You can simplify your selector as

$('.marker-tooltip-container section[data-locationid="' + location.id + '"] h6').text(val);

OR

$('.marker-tooltip-container section').filter('[data-locationid="' + location.id + '"]').find('h6').text(val);
Sign up to request clarification or add additional context in comments.

Comments

2

Upate your code to

$($('.marker-tooltip-container section').filter('[data-locationid="' + location.id + '"]').find('h6')[0]).text(val);

Basically to get the h6 element in your case in place of .('h6'), you need to use .find('h6'). This will give you all the h6 nodes, hence, to update, first node you have to use .find('h6')[0]. However, if there is only 1 node or you want to update all the nodes, simply, use .find('h6')

Additionally, you can also try with this as well. The above rules applies here as well.

$('.marker-tooltip-container section[data-locationid="' + location.id + '"] h6').text(val);

5 Comments

@Satpal - If you have slightest of feeling of being copied, let me know again - I will delete the answer
I really don't get you. I am still updating my answer. I will remove the second approach considering you think it as copied, though it is not.
Well, your interruption stopped my flow of updating the answer :P
You don't need to use [0] unless you want to select first element. as $('.marker-tooltip-container section').filter('[data-locationid="' + location.id + '"]').find('h6') is already jQuery object so you can directly use .text() method
I agree with you. However, there is no markup provided so I have to put in more details. Anyways thanks for your advise.

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.