-1

I have this div:

<div class="transparency" data-title="<? echo $perf; ?>">&nbsp;</div>

An this is the jquery code which extracts the data-title value and replaces the &nbsp with it.

function performerNames(){

    var performername = $(".transparency").data('title'); 

    var divcontent = $(".transparency").innerHTML;

    if (divcontent == '&nbsp;'){

        $(".transparency").html(''+divcontent+''); 

    }

}

Ty very much!

1
  • Have you actually called performerNames() somehow? Commented May 4, 2011 at 16:51

4 Answers 4

4

Use html() instead of innerHTML::

var divcontent = $(".transparency").html();

and you want to replace by data, so:

$(".transparency").html(''+performername+'');

instead of:

$(".transparency").html(''+divcontent+''); 

UPDATE:

whole code:

function performerNames(){    
    var performername = $(".transparency").data('title');     
    var divcontent = $(".transparency").html();    
    if (divcontent == '&nbsp;'){    
        $(".transparency").html(''+performername+'');     
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

The innerHTML should not ever be &nbsp; unless you double entity encoded your non-brekaing space. Thus the body of your if block does not execute.

EDIT: This, as suggested by others, works for me:

function performerNames()
{
    var $transparency = $( '.transparency' ),
        performername = $transparency.data( 'title' );

    if( $transparency.html() == '&nbsp;' )
    {
        $transparency.html( performername );
    }
}

2 Comments

Yup, I guess the .html() would return an empty string on that, right?
No, it shouldn't. As others here mentioned, it should return the &nbsp;. In my edit you can see code that works for me as suggested by others. If it didn't return the &nbsp; it should return the actual non-breaking space char which that entity represents.
0

Replace $(".transparency").innerHTML; with $(".transparency").html();

Comments

0

You're just setting the html content to be the same as it is:

var divcontent = $(".trasparency").html();
$(".transparency").html(divcontent);

Which should be

$(".transparency").html(performername);

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.