-5

I really need to know the input's attribute when document had finished loading. For example:

<input type="text" data-type="string" />
<input type="text" data-type="string" />
<input type="text" data-type="date" />

And with javascript with an alert should display

you have 2 string data-type and 1 date data-type
Misters
  • 1,337
  • 2
  • 16
  • 29

2 Answers2

0

with Jquery.

$.each( $("input") , function(_index, _item){    
    alert($(_item).attr("data-type"));
});

you just now have to count and display your message...

ncubica
  • 8,169
  • 9
  • 54
  • 72
  • for complex data you can use `$(_item).data('type')` [differences](http://stackoverflow.com/a/7262427/2033671) –  Mar 15 '13 at 20:35
0

At the risk of being slated for giving you too much code... given that your question hasn't been tagged jQuery a pure Javascript implementation could be as follows:

(function(){
    var loader = window.onload;
    window.onload = function(){
        if(typeof loader == 'function') loader();
        var els = document.getElementsByTagName('input'), data = [];
        for(var i=0;i<els.length;i++){
            var type = els[i].getAttribute('data-type') || 'unknown';
            if(typeof data[type] == 'undefined') data[type] = 1;
            else data[type]++;
        }
        var out = '';
        for(var key in data){
            if(out != '') out += 'and '; else out += 'You have ';
            out += data[key] + ' ' + key + ' data-type ';
        }
        alert(out);
    }
})();

Somewhat more convoluted and perhaps confusing - but should you post any further questions please include code examples of what you have tried and state where exactly you are fuzzy.

Emissary
  • 9,954
  • 8
  • 54
  • 65