paulfah
paulfah

Reputation: 113

Change ASP Textbox Property with Javascript

So I have this ASP.NET form which contains TextBoxtA and TextBoxtB. I want that if I enter certain input on TextBoxA (for example '000') the TextBoxtB text value would be set to 'TextChanged' and disabled.

PS: And I want that happen right after the text di TextBoxA changes, not when onClick fired. So I use the attribute onTextChanged. According to this http://forums.asp.net/t/1300979.aspx, I have to use onChange instead and change the poperty on PageLoad on my code behind. But it can't seem to work.

So heres my code:

<script runat="server">
    function changeText() {
        var A = document.getElementById('<%=inputTexboxtA.ClientID%>').value;
        var B = document.getElementById('<%=inputTexboxtB.ClientID%>').value;

        if (A = '000') {
            B = 'TextChanged';
        }
    }
</script>

protected void Page_Load(object sender, EventArgs e)
            {
                inputTexboxtA.Attributes.Add("onChange", changeText());
            }

<asp:TextBox ID="inputTexboxtA" runat="server" MaxLength="3"/></td>
<asp:TextBox ID="inputTexboxtB" runat="server" MaxLength="3"/></td>

Am I doing it right? Thanks before!

Upvotes: 0

Views: 2090

Answers (2)

Jidheesh Rajan
Jidheesh Rajan

Reputation: 4845

protected void Page_Load(object sender, EventArgs e)
        {
            inputTexboxtA.Attributes.Add("OnKeyUp", changeText());
        }

Upvotes: 0

Jon P
Jon P

Reputation: 19772

Basicaly no, you're doing it wrong. OnChange is not an event for text boxes. Look at OnBlur for when the user changes field or OnKeyUp for when the user is typing for other options. Potentialy use both.

You may also want to investigate jQuery

You also have a syntax error and other problems. You need to use == or != for comparison. Also you aren't change the value of B so:

function changeText() {
    var A = document.getElementById('<%=inputTexboxtA.ClientID%>').value;
    var B = document.getElementById('<%=inputTexboxtB.ClientID%>');

    if (A == '000') { // or (A != '000')
        B.value = 'TextChanged';
    }
}

OnBlur example: http://jsfiddle.net/4HwmU/

Update: So I shouldn't code when tired! OnChange is fine the problem was the various errors in the code

OnChange Example: http://jsfiddle.net/4HwmU/1/

For More info on the differnces between OnBLur and on Change: What is the difference between onBlur and onChange attribute in HTML?

Upvotes: 1

Related Questions