1
<div class="elem">
    <div data="1">
    <div data="11">test1</div>
        <div data="12">test2</div>
    </div>
    <div data="2">
        <div data="21">test3</div>
        <div data="22">test4</div>
    </div>
</div>

in above example I would like to print data value of each element , so please help me for this

0

2 Answers 2

1

Try this: You can find all divs inside div with class="elem", then iterate all divs to read data attribute using .each()

$(function(){
   $('div.elem div').each(function(){
     alert($(this).attr('data'));
   });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="elem">
    <div data="1">
    <div data="11">test1</div>
        <div data="12">test2</div>
    </div>
    <div data="2">
        <div data="21">test3</div>
        <div data="22">test4</div>
    </div>
</div>

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

1 Comment

@support If answer help you then please mark and up vote the answer.
1

data is used as a suffix for setting data property to element. You should use it as data-propname

and then use .data() to get them:

$('.elem div').each(function(){
    console.log($(this).data('propname'))
});

3 Comments

Shouldn't it be .data('propname')?
@Barmar: indeed yes.. :)
its not exact the code that will run but it helped me to understand

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.