Skip to content
Advertisement

Get id by finding element from data in a dynamic html (jquery)

I am trying to get the element id during scan barcode sku event, and get element according to data-sku in the li which added dynamically under UL which loads from another function on load of page.

I tried many ways as guided but it looks i am missing some thing due to which i am unable to get desired result, following what i am trying is the last looks more relative.

Data i have in UL added dynamically in another function on load

JavaScript

Lastly i have called following script to select element from data-sku=”ORD_3932_4051″ and log it into the console

JavaScript

I got the below result in the log (its not complete array but a overview to understand),which seems not as it should be

JavaScript

And finally when i am trying to fetch id from this it does shows undefined

Please Note: When i am only calling UL elements as below it gives all element in child nodes.

JavaScript

result for above i got in firefox is as below (its not complete as its large but to give you an idea)

JavaScript

The complete snippet you can see below:

JavaScript
JavaScript

Applied Changes as specified in comment I tried following with children()

JavaScript

it gives following nodes but not selecting specific element by data-sku https://i.ibb.co/JtpW0q7/image.png

i am curious if there is a way to get the element from the array or object by key after finding specific data-sku of it

Solution Found

As Mr @uingtea guided that , find the right selector via inspect element so i did that, and found it was mistake in the selector i used double quotes in inspect element and then apply same in script and it works.

JavaScript

Advertisement

Answer

.find() return array [] you need to define the index

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement