0

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?

2 Answers2

1

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.

David
  • 208,112
  • 36
  • 198
  • 279
  • I had confirmed that the IDs were acting as expected. It was the uppercase V causing my issue. Is ClientIDMode acceptable for most browsers or should I do this style: <%# xxxx.ClientID%> in the javascript? – Jordan Wayne Crabb Oct 15 '15 at 19:25
  • @JordanWayneCrabb: It shouldn't be browser-dependent. The biggest consideration to be made is the fact that `id` values need to be unique on the page. So you might need to make adjustments when using server-side controls inside of things like repeaters or grids. – David Oct 15 '15 at 19:26
0

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

haraman
  • 2,744
  • 2
  • 27
  • 50