useranon
useranon

Reputation: 29514

How to set a value for a span using jQuery

How to set a value for a <span> tag using jQuery…

For example…

Below is my <span> tag:

<span id="submittername"></span>

In my jQuery code:

jQuery.noConflict();
    
jQuery(document).ready(function($){

    var invitee = $.ajax({
        type: "GET",
        url: "http://localhost/FormBuilder/index.php/reports/getInvitee/<?=$submitterid;?>",
        async: false
    }).responseText;

    var invitee_email=eval('(' + invitee + ')');
    var submitter_name=$.map(invitee_email.invites, function(j){ 
        return j.submitter;
    });         
    alert(submitter_name); // alerts correctly 
    $("#submittername").text(submitter_name); //but here it is not working  WHy so??????
});

Upvotes: 434

Views: 764748

Answers (6)

E Demir
E Demir

Reputation: 81

Syntax:

$(selector).text() returns the text content.

$(selector).text(content) sets the text content.

$(selector).text(function(index, curContent)) sets text content using a function.

Source: https://www.geeksforgeeks.org/jquery-change-the-text-of-a-span-element/

Upvotes: 8

Jorge Santos Neill
Jorge Santos Neill

Reputation: 1785

The solution that work for me is the following:

$("#spanId").text("text to show");

Upvotes: 6

Rita Chavda
Rita Chavda

Reputation: 191

You can use this:

$("#submittername").html(submitter_name);

Upvotes: 9

Santosh Ganacharya
Santosh Ganacharya

Reputation: 386

You are using jQuery(document).ready(function($) {} means here you are using jQuery instead of $. So to resolve your issue use following code.

jQuery("#submittername").text(submitter_name);

This will resolve your problem.

Upvotes: 21

Steerpike
Steerpike

Reputation: 17544

You're looking for the wrong selector id:

 $("#submitter").text(submitter_name);

should be

 $("#submittername").text(submitter_name);

Upvotes: 17

cletus
cletus

Reputation: 625007

You can do:

$("#submittername").text("testing");

or

$("#submittername").html("testing <b>1 2 3</b>");

Upvotes: 860

Related Questions