140

I have a really simple question but it's something I have not done before. I have the following:

<td id="abc" style="width:15px;"><span></span></td>

I would like to put some text into the span.

How can I do this with jQuery. I know how to change things with the id of abc but not the code inside the span.

thanks,

Ileana Profeanu
  • 377
  • 2
  • 10
  • 33
Christine Mayes
  • 1,427
  • 2
  • 10
  • 6
  • 6
    Have a look at jQuery's [selector documentation](http://api.jquery.com/category/selectors/). – Felix Kling Aug 28 '11 at 16:32
  • 2
    @Felix. +100 . A link to the API worths more than a 'muted' line of sample code. – Roko C. Buljan Aug 28 '11 at 17:17
  • 1
    Possible duplicate of [how to set a value for a span using JQuery](http://stackoverflow.com/questions/1491743/how-to-set-a-value-for-a-span-using-jquery) – Stewart Apr 01 '17 at 15:47

6 Answers6

221
$('#abc span').text('baa baa black sheep');
$('#abc span').html('baa baa <strong>black sheep</strong>');

text() if just text content. html() if it contains, well, html content.

sampathsris
  • 21,564
  • 12
  • 71
  • 98
dexter
  • 13,365
  • 5
  • 39
  • 56
59

This will be used to change the Html content inside the span

 $('#abc span').html('goes inside the span');

if you want to change the text inside the span, you can use:

 $('#abc span').text('goes inside the span');
Zeeshan Adil
  • 1,937
  • 5
  • 23
  • 42
Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
16
$('#abc span').html('A new text for the span.');
scessor
  • 15,995
  • 4
  • 43
  • 54
13

Try this

$("#abc").html('<span class = "xyz"> SAMPLE TEXT</span>');

Handle all the css relevant to that span within xyz

ajup
  • 330
  • 2
  • 12
-1

Syntax:

  • return the element's text content: $(selector).text()
  • set the element's text content to content: $(selector).text(content)
  • set the element's text content using a callback function: $(selector).text(function(index, curContent))

JQuery - Change the text of a span element

IGP
  • 14,160
  • 4
  • 26
  • 43
E Demir
  • 81
  • 1
  • 2
  • Please do not re-post copies of other answers (even if they are your own answers). Also, this answer could be improved formatting the code correctly (or at least commenting out non-code), and by using specific selectors as an example. – Thomas F Feb 24 '21 at 18:15
-1

$('#abc span').html("your new string");