We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

[Solved] Having trouble programatically setting the text in a TextBox

Hello,

Below is a portion of some code I'm using to help learn how to use the syncfusion controls. It creates a testbox and a button and each time the button is pressed it increments a counter and displays its value in the button caption and in the textbox value. It displays the text in the button caption correctly, but the textbox doesn't seem to get updated.

Using chrome and looking at the page source, the textbox value is being updated but its not being updated on the screen.

Am I missing something ?

<ejs-textbox id="play" placeholder="Play Stuff" value="@ViewBag.Play" enablePersistence="false" multiline="false"></ejs-textbox>
<ejs-button id="myButton" content="Press Me Now !" onClick="Pressed();"></ejs-button>

<script>
    var gbl = 1;
    function Pressed() {
        $.ajax({
            url: '@Url.Action("Zap")',
            dataType: "json",
            type: "POST",
            success: function (data) {
                gbl++;
                var play = $("#play");
                play.attr("value", gbl.toString());
                play.text("dddddd");

                var but = $("#MyButton");
                but.text(gbl.toString());
            }
        });
    }

</script>


3 Replies

AN Andrew May 31, 2019 02:14 PM UTC

I seem to have stumbled on a solution.

If I remove the 

value="@ViewBag.Play"

then the script is able to update the value.

But if I then enter text into the field, it's no longer able to update the value. Not sure what I'm doing wrong here.

A more complete example is in the attachment (I couldnt figure out how to escape the sample and it kept getting truncated when I pasted it in).


Attachment: sample_ca627274.zip


AN Andrew June 1, 2019 12:37 PM UTC

I found my problem. I was trying with text and html in the jqurey portion and I should have been using val


PO Prince Oliver Syncfusion Team June 3, 2019 07:04 AM UTC

Hi Andrew, 

Greetings from Syncfusion support. 

Using the value property in the control that is accessible via the instance of control, you can programmatically set the text in a TextBox. Please refer to the following code. 

<script> 
    var gbl = 1; 
    function Save() { 
        $.ajax({ 
            url: '@Url.Action("Zap")', 
            dataType: "json", 
            type: "POST", 
            success: function (data) { 
                gbl++; 
                var desc = document.getElementById("description").ej2_instances[0]; 
                desc.value = gbl; // Use this instead of the below code 
                // desc.attr("value", gbl.toString()); 
            } 
        }); 
    } 
</script> 

Let us know if you need any further assistance on this. 

Regards, 
Prince 


Loader.
Live Chat Icon For mobile
Up arrow icon