JQuery - each and data ()

how can i get all the "rt_XX" with jquery? I have no idea: / .. I hope someone can help me.

$("body").data('conf_pkc', 'normal');
$("body").data('conf_sst', '0');
$("body").data("rt_01", { tw:'k25', title:'Bernd', descr:'man' });
$("body").data("rt_12", { tw:'k115', title:'Hugo', descr:'man' });
$("body").data("rt_55", { tw:'k25', title:'Jan', descr:'man' });

/*    
    $.each( XXXXXXXXXXXX , function(k, v){

        var rt_tw = $('body').data('rt_01').tw;
        var rt_tw = $('body').data('rt_01').tw;        

        $('#d1').append('rt_tw  -> '+rt_tw+'<br />');
        $('#d1').append('rt_title  -> '+title+'<br />');        
        $('#d1').append('rt_descr  -> '+descr+'<br />');

    });

*/  

http://www.jsfiddle.net/V9Euk/543/

Thanks in advance. Peter

+3
source share
4 answers

If you are using jQuery 1.4+, you should do something like this:

var data = $("body").data();

for (p in data) {
   if (p.substring(0,3) == "rt_") {
      var x = data[p];
      alert(x.tw);
      alert(x.title);
      alert(x.descr);
   }
}

Just test it on your link. It works as intended!

Good luck

+4
source

Since there is only one <body>, there is no need to bind data with it (if the data is not used by any third-party widgets / plugins).

All data can be stored in some global configuration object or even in wide area networks - or it will be better than overhead $('body').data('foo').

:

var rt = []; // An array

rt[12] = {...};
rt[55] = {...};

: ({}) - .

+1
$("body").data('conf_pkc', 'normal');
$("body").data('conf_sst', '0');
$("body").data("rt_01", { tw:'k25', title:'Bernd', descr:'man' });
$("body").data("rt_12", { tw:'k115', title:'Hugo', descr:'man' });
$("body").data("rt_55", { tw:'k25', title:'Jan', descr:'man' });


var b    =  $('body');
var data = b.data();
var arey = [];

for(var i in data) {
   if(i.match(/rt_?\d{2}/) ) {
        arey.push(i);
    }
}

$.each(arey, function( index, val ) {
   var item = $('body').data( val )
    for(var i in item ) {
      (function(t) {
        console.log(val + ' => '+ t+ ' : ' +item[t]);
      })(i);
    }
});

, Firebug, =)

Demo : http://jsbin.com/ufoza3

+1
source

Try the following:

value = jQuery.data($("body").find("xxx")[0], "rtxx)");

You will need to skip items if you are not using [0]. This is just for you.

-1
source

Source: https://habr.com/ru/post/1763961/


All Articles