0

Ok, the title might be a little confusing. Let me explain.

<div class="book">
    <h3>Name of Book</h3>
    <p>
        <span data-notmobile="Author"></span>Rajesh K. Maurya</p>
    <p>
        <span data-notmobile="Publication"></span>Wiley India</p>
    <p>
        <span data-notmobile="Edition"></span>2011</p>
    <p>
        <span data-notmobile="Branch"></span>Information Technology</p>
    <p>
        <span data-notmobile="Semester"></span>5</p>
</div>

Now I want a way to swap out the content of each span with its data attribute respectively. I tried

$('.book span').html($('.book span').data("notmobile"));

which only changes the inner html to the first attribute ie "Author". For some reason, the 'this' keyword does not work.

I want to do this without giving each span a class of its own.

1
  • 1
    PS. Where's "this" in your code..? Commented Mar 25, 2012 at 10:50

3 Answers 3

3
$('.book span').each(function(){
    $(this).html($(this).data("notmobile"));
});

LIVE DEMO

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

1 Comment

@supernoob. No problem. I added a demo, hope you like it.
2

$('.book span').data('notmobile') returns the value of the first matching element. Use the following code to get the correct desired effect:

Demo: http://jsfiddle.net/Gezxj/

$('.book span').html(function() {
    return $(this).data("notmobile");
});

1 Comment

This fiddle is easy to get. just added css to your fiddle
1
$('.book span').each(function() {
    var $this = $(this);
    $this.html($this.data('notmobile')); 
});

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.