1

Hi I am getting all the a tag attributes using jquery function, now I want to store them into one and append in body, but cant find the way how to get it done. Here is fiddle link

<head>
<script type="text/javascript">
$(function(){
$('a').click(function(){
var name= $(this).attr('name');
var href= $(this).attr('href');
var jj= $(this).attr('class');
var user_id= $(this).attr('user_id');
var page_type= $(this).attr('page_type');
var price= $(this).attr('price');
var bonus= $(this).attr('bonus');
var wrapper= $(this).attr('wrapper');
var token= $(this).attr('token');
var own= $(this).attr('own');
$('body').append('<div>'+name+'<div>')
})})
</script>
</head>
<body>
<a id="profile_2902" name="b_now" href="#" class="big_now" user_id="152402569967" page_type="profile" price="292" bonus="0" wrapper="5972569967" token="e644ce48aa43dc3caa348745a" own="4100447132">
Click now!
</a>
</body>
1
  • Can you please show us a sample of what you want the output to be? Commented Aug 27, 2012 at 13:18

3 Answers 3

1

You could use raw js to get all attributes and print them with jquery like this:

var el = document.getElementById("someId");
var arr = [];
for (var i=0, attrs=el.attributes, l=attrs.length; i<l; i++){
    arr.push(attrs.item(i).nodeValue);
}
$('body').append('<div>'+arr.join(", ")+'<div>');

Working fiddle example here

Sign up to request clarification or add additional context in comments.

7 Comments

What is item in your function and what is the use of it
It gets an item from the attrs array
is item is method of javascript ? and why we cant use attrs[i]
The item() method returns the node at the specified index in a node list. see: w3schools.com/dom/met_nodelist_item.asp
can we use attrs[i] instead of item
|
1

You can use this function below to read all attributes and push them in array..

DEMO: http://jsfiddle.net/cuyfK/1/

    var el = this, arr = [], it;
    for (var i = 0, attrs = el.attributes, l = attrs.length; i < l; i++) {

        it = attrs.item(i);

        if (it.nodeName == 'id') {
            arr.push(it.nodeName + '="' + it.nodeValue + '_fixed"');
        } else {
            arr.push(it.nodeName + '="' + it.nodeValue + '"');
        }
    }

or May be you want to push specific attr values inside an array.. Try below,

$(function() {
    $('a').click(function() {
        var attr = [];
        attr.push($(this).attr('name'));
        attr.push($(this).attr('href'));
        attr.push($(this).attr('class'));
        attr.push($(this).attr('user_id'));
        attr.push($(this).attr('page_type'));
        attr.push($(this).attr('price'));
        attr.push($(this).attr('bonus'));
        attr.push($(this).attr('wrapper'));
        attr.push($(this).attr('token'));
        attr.push($(this).attr('own'));

        $('body').append('<div>' + attr.join(' ') + '<div>')   
    });
});

1 Comment

Why you use item in your function
1

Use an array to store the attributes you wish to look up, and then just iterate over that array with $.each to find all the values and concentenate them into a string and append them all in one go to be more efficient :

$(function(){
    var attributes = ['name', 'href', 'class', 'user_id', 'page_type',
                      'price', 'bonus', 'wrapper', 'token', 'own']

    $('a').on('click', function(){
        var html='', self=this;
        $.each(attributes, function(i,e) {
            html += '<br><div>'+e+' : '+$(self).attr(e);
        });
        $('body').append(html);
    });
});​

FIDDLE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.