You must Sign In to post a response.
  • Category: JQuery

    How to assign value to Span and div using jQuery ?


    Are you looking for a way to assign value to Span and div using jQuery ? then read this thread to know more about it



    Hi,i am working on asp.net mvc razor view,i am using partial views,
    i am placed span control in one partial view,i am binding partial view
    in another view with jquery but it is not working,can any one guide me pls,
    paritalvuew1.cshtml
    <td><b>


    <span id="spnPackageName" ></span>

    <span id="submittername"></span>
    </b>
    <div id="divPackageName66666"></div>


    </td>

    in another partial view

    jQuery.noConflict();
    $(document).ready(function () {
    $(".hpConsumerlink").click(function (e) {
    debugger;


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


    $("#divPackageName66666").append("sai ram");

    });

    });
  • #755399
    please write your jquery code in another js file which will be your jquery file containing all the jquery code and then link this jquery file in the _Layout.cshtml file.
    In the js file on document.ready, write the jquery code. debug and see if it is able to find the control with the id you have specified.

    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #755403
    Hi priya ,here event is firing but value is not assigning

  • #755414
    try with innertext or html in case of alert($("#spnPackageName").text())

  • #755456
    for the span tag you need to set its innerHtml property value not the text or value property.

    get the reference to the elements and set its innerHtml property to the required value.

    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #755484
    For DIV and span the innerHTML property is the best way to set the values foe example



    $("#spanName").html("Hello World");
    $("#dvName").innerHTML("<b> Hello Anil"</b>);

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #757558
    Hi,

    generally html controls which display text will set with value or text property but when assigning value to the container control div and span is different because it has a capability of displaying some other controls in which so

    for these kind of controls if we want to display text content we need to set with the property of html

    in this case the problem is it will clear previous content from which and display the current assign value so in this case we must be a bit care full while assigning values.

    please check the code snippet for the reference.

    $("#divid").html("Hi World");

    $("#spanid").html("i am a span ");

    Regards
    SriSunny


  • Sign In to post your comments