Skip to content
Advertisement

How to get dynamic value from html tag with foreach and save it to jquery variable for momentObj

Honestly i am not sure how to make a suitable title for this but basically i was trying to extract element from span tag with id=duration which comes dynamically from database with foreach, and use it to display timer for a duration with countdown.js, but i am confused as it is only extracting the first foreach value and not the rest beacause it has the same id i think. this is the view

JavaScript

Advertisement

Answer

Since IDs must be unique only use a common class name. In addition you need to change each instance which for this case you can use text(function) which internally will loop over each instance.

Then you also want to start the initial start_time which you can do using a data- attribute. Now whenever you call update() you can get the initial value from that attribute and do calculations based on it.

Rather than manufacture some dates/times I created a very simple case that just adds one when called

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