133

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
  • 370
  • 2
  • 8
  • 32
Christine Mayes
  • 1,357
  • 2
  • 9
  • 6
  • 5
    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

204
$('#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
  • 20,518
  • 11
  • 61
  • 93
dexter
  • 12,937
  • 5
  • 36
  • 56
58

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,765
  • 4
  • 20
  • 40
Alan Haggai Alavi
  • 69,934
  • 19
  • 98
  • 125
16
$('#abc span').html('A new text for the span.');
scessor
  • 15,965
  • 4
  • 40
  • 53
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
  • 10,505
  • 3
  • 19
  • 35
E Demir
  • 61
  • 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");