Jordan Wayne Crabb
Jordan Wayne Crabb

Reputation: 319

Get/Set asp:textbox values from javascript

I'm not super familiar with javascript but I'm working on it, I'm trying to do a "same as billing address" type checkbox to fill some textboxes on the same form with data from other textboxes. I found a few solutions online but they weren't working for me. I'm probably overlooking something simple, but I've tried quite a few. Here's what I currently have:

function AutoFillBilling()
    {
        var text = document.getElementById("CustContact").Value;
        alert(text); 

    }

The alert pops up, but just says undefined, I also tried $("#CustContact").Value to no avail. Below is the textbox I'm trying to access

<asp:Textbox runat="server" ID="CustContact" ClientIDMode="Static" type="text" placeholder="Contact" class="required"/>

What am I missing?

Upvotes: 0

Views: 2321

Answers (2)

haraman
haraman

Reputation: 2742

You can use it as such

<asp:CheckBox ID="Postal_Same_As_PermCheckBox" runat="server" onclick="copyPermAddress(this);" />

And JavaScript as

<script type="text/javascript">
function copyPermAddress(e) {
    if (e.checked) {
        document.getElementById('Postal_Adrs_Line_1TextBox').value = document.getElementById('Perm_Adrs_Line_1TextBox').value;
        document.getElementById('Postal_City_VillageTextBox').value = document.getElementById('Perm_City_VillageTextBox').value;
    } else {
        document.getElementById("Postal_Adrs_Line_1TextBox").removeAttribute('readonly', 'readonly');
        document.getElementById("Postal_City_VillageTextBox").removeAttribute('readonly', 'readonly');
    }
}

In this example I am assuming that you are using client id mode as static on the controls mentioned in the JavaScript, if it is not then you may use <%# xxxx.ClientID%> blocks to access their IDs

Upvotes: 0

David
David

Reputation: 218818

Properties begin with lowercase letters in JavaScript:

var text = document.getElementById("CustContact").value;

Additionally, while the ClientIDMode="Static" certainly should be explicitly setting the client-side id property, when debugging you may want to examine the HTML just to make sure. When using JavaScript, looking only at your server-side markup leaves you unnecessarily blind.

Upvotes: 1

Related Questions