L84
L84

Reputation: 46308

Modifying innerHTML from 3rd Party Script

I have a 3rd party script that is importing a menu and I cannot edit this 3rd party script. It generates code like this:

<div id="wmenu-updated" style="display: block;">
  <small>Menu updated</small>
  <span innerhtml="19 hours ago"></span>
</div>

It should take 19 hours ago and display that as text inside the span but for whatever reason it doesn't work and the makers of the script are little help in fixing the error.

Is there a way, using jQuery, that once the page loads I can take the innerhtml and spit it out as text inside that span?

Upvotes: 11

Views: 396

Answers (6)

bobthecow
bobthecow

Reputation: 5117

Lots of people are getting it close :)

This will do it for you:

$('selector').find('span[innerhtml]').each(function() {
    $(this).html($(this).attr('innerhtml'));
});

jsFiddle here.

And this is functionally equivalent:

$('selector').find('span[innerhtml]').html(function() {
    return $(this).attr('innerhtml');
});

Upvotes: 4

Jai
Jai

Reputation: 74738

You can try this one: fiddle

 $(function(){
    $(document).children('#wmenu-updated').find('span[innerhtml]')
    .text($(this).attr("innerhtml"));
 });

or more simple like this:

$('span[innerhtml]').text($('span[innerhtml]').attr('innerhtml'));

Upvotes: 4

Tepken Vannkorn
Tepken Vannkorn

Reputation: 9713

Try this:

$(document).ready( function ( e ) {
    var q = $('span').attr('innerhtml');
    $('#wmenu-updated').children('span').text( q );
});

Upvotes: 4

Hary
Hary

Reputation: 5818

Use $.attr() and $.text() to achieve this

$(document).ready(function(){
   var txt = $("#wmenu-updated span").attr("innerhtml");
   $("#wmenu-updated span").text(txt);
});

Upvotes: 6

mika
mika

Reputation: 1451

$("#wmenu span").attr("innerhtml", "Your Update here");

Upvotes: -1

whitneyit
whitneyit

Reputation: 1226

$( document ).ready( function () {
    $( '#wmenu-updated span' ).text( function () {
        return $( this ).attr( 'innerhtml' );
    });
});

You can use jQuery's text function to update the text in the span

Upvotes: 6

Related Questions