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?
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?
6 Answers
Reset to default 6$( 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
Use $.attr() and $.text() to achieve this
$(document).ready(function(){
var txt = $("#wmenu-updated span").attr("innerhtml");
$("#wmenu-updated span").text(txt);
});
Try this:
$(document).ready( function ( e ) {
var q = $('span').attr('innerhtml');
$('#wmenu-updated').children('span').text( q );
});
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'));
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');
});
$("#wmenu span").attr("innerhtml", "Your Update here");