shahid hamdam
shahid hamdam

Reputation: 821

getting values from same id in while loop

I have a while loop in which i am getting different dates against each event.

 <?php while( have_rows('_event_date_time_slots') ): the_row(); ?>

 <div>
     <h3 class="date-<?php echo $post->ID?>" 
         name="tttdate<?php echo $post->ID?>"           
         value="<?php the_sub_field('_event_date'); ?>">
 <?php the_sub_field('_event_date'); ?></h3>

I have assigned class to h3 element to get dates in jquery with this code.

 var t_date = jQuery('.date-<?php echo $post->ID?>').html();

but it returns only first item. Then i assign name to h3 element and try to get dates by this code.

var tttdates = document.getElementsByName("tttdate<?php echo $post->ID?>");
var vals=[];
for (var i=0, n=tttdates.length; i<n; i++) {
     vals.push(tttdates[i].value);
}
alert(vals.join(","));

Now I am getting only , or empty alert box. where I am doing mistake?

Upvotes: 2

Views: 481

Answers (2)

Jaymin Panchal
Jaymin Panchal

Reputation: 2856

You can iterate through the same class using each.

Try this

$('.date-<?php echo $post->ID?>').each(function(i, obj) {
    vals.push($(obj).text());
});
alert(vals.join(","));

Upvotes: 0

Satpal
Satpal

Reputation: 133403

You need to use textContent property, as tttdates[i] refer's <h3> which doesn't have value property.

vals.push(tttdates[i].textContent);

As you are using jQuery, You can use .map()

var vals = $(".tttdate<?php echo $post->ID?>").map(function(){
    return this.textContent;
}).get();

Upvotes: 1

Related Questions