tech74 tech74 - 6 months ago 13
jQuery Question

JQuery change text of paragraph which contains image

I have the following html

<p id="logo400_120_spinner"><img src="/global/img/spinner.gif"></p>


I use jquery to try to set the text to Loading or removing depending on circumstance but I am just getting the text and losing the spinner image. I would like the spinner to be present with text on the right hand side

Here is the jquery I use

$("#logo400_120_spinner").text('Loading');
$("#logo400_120_spinner").show();


The spinner image disappears with the above.

Answer

Use .html() to change the inner HTML instead of just the plaintext so you can use an img tag

To show:

 $("#logo400_120_spinner").html('<img src="/global/img/spinner.gif">Loading');
 $("#logo400_120_spinner").show();

To get rid of the loading text:

$("#logo400_120_spinner").html('<img src="/global/img/spinner.gif">');

Another option is just to put the "Loading" text into a span and just show and hide that span with JQuery.

Comments