[jquery] 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??????
});

This question is related to jquery

The answer is


You can use this:

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

You're looking for the wrong selector id:

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

should be

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

The solution that work for me is the following:

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

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.


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.

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