The class don't remain on page refresh, i use jquery cookies plugin

    <script type="text/javascript">
jQuery(function($) {
        var cookieName = 'SalveazaIDtr_';

        if (cookieName) {
            $('#' + cookieName).addClass('selectListItemReaded');
        }
        $('.selectListItem').bind('click', function(e) {

            var id = $(this).attr('id'),
                cookie = cookieName + id;


            $(this).addClass("selectListItemReaded");
            $.cookie(cookieName + $(this).attr('id'), id, {
                expires: 7
            });

        });
    });
});
</script>

Hi, i have set the script for multiple cookies for each link, the class .selectListItemReaded it’s added on click but it’s not persist on page refresh. The cookies are saved , I tested in firebug.

Ex: Cookie

Name: SalveazaIDtr_child-0
Value: child-0
Expire:07/16/2015, 12:06:17 PM

What is really mistake in my code ?

Thanks

Could you supply enough code (i.e. markup) that we can recreate your problem?

that is because you do not read the cookie at page load. the first 5 line are completely useless as 1) the condition is always true 2) the cookie name has the id added but in the assignment the cookie template name is used (i.e. without the id).

Note:

$(this).attr('id') == this.id

This code create automatically id for

<script type="text/javascript">

jQuery(function ($) {
   
 var selectListItemDivs=$("tr.selectListItem");
 for(var i=0;i<selectListItemDivs.length;i++)
 {  
    $(selectListItemDivs[i]).attr("id","child-"+i);
 }

});
</script>

Here it’s the table:

<table cellpadding="1" cellspacing="1" width="100%" id="tabelRezultat">
<tbody>
<tr class="selectListHeader">
<td class="selectListTitle inceput">Data cursei libere</td>
<td class="selectListTitle">Plecare din</td>
<td class="selectListTitle">Cursa spre</td>
<td class="selectListTitle">Locuri disponibile</td>
<td class="selectListTitle"></td>
</tr>
<?php
if ( get_query_var('paged') ) $paged = get_query_var('paged');  
if ( get_query_var('page') ) $paged = get_query_var('page');
 
$query = new WP_Query( array( 'post_type' => 'curse', 'paged' => $paged ) );
 
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>	
<tr class="selectListItem" height="30">
<td class="rezultat inceput"><?php print_custom_field('dataCursaLibera'); ?></td>
<td class="rezultat"><?php print_custom_field('orasPlecareCursaLibera'); ?></td>
<td class="rezultat"><?php print_custom_field('orasCalatorie'); ?></td>
<td class="rezultat"><?php print_custom_field('locuriCurseLibere'); ?></td>
<td class="rezultat"><a id="clickBut" target="_blank" href="<?php echo get_permalink(); ?>">detalii</a></td>
<?php the_content(); ?>
	<?php endwhile; wp_reset_postdata(); ?>
	<!-- show pagination here -->
<?php else : ?>
	<!-- show 404 error here -->
<?php endif; ?>
</tr>
</tbody>
</table>

and here the script for cookies

<script type="text/javascript">
jQuery(function ($) {

var cookieName = 'SalveazaIDtr_';

    if (cookieName) {
        $('#'+cookieName).addClass('selectListItemReaded');
    }
$('.selectListItem').bind('click', function(e) {
											  
          var id = $(this).attr('id'), cookie = cookieName + id;
		
        
        $(this).addClass("selectListItemReaded");
            $.cookie(cookieName + $(this).attr('id'),  id, { expires: 7 });

													});
						   });

</script>

Can you help me with this, i need the final code because I’m stuck . Thanks guys

Can you help me with correct code?

var cookieName = 'SalveazaIDtr_';

$('.selectListItem').bind('click', function(e) { ...

you would have to check your cookie plugin how to read cookies and get the ids from there. then add the class for each of the found ids.

The plugin it’s https://github.com/carhartl/jquery-cookie

in documentation they say:

Read all available cookies:

$.cookie(); // => { "name": "value" }

then you would use that to get a hold of the ids that should get the class added.

Can u give me a sample code because I am beginer to jquery and don’t know how to begin the code . Thanks

start with

console.log($.cookie());

that will give you the base information to proceed.

That is why instead of giving you a fish, we are attempting to teach you how to fish instead. For there are a saying that goes something like:

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.