Easy JS Question

Last post 01-18-2006 7:08 AM by sconard. 7 replies.

Sort Posts:

  • Easy JS Question

    01-04-2006, 11:48 AM
    • Participant
      1,430 point Participant
    • ech01
    • Member since 09-08-2004, 3:19 PM
    • MN
    • Posts 288

    I can't seem to remember how to add two text box values together and display the answer dynamicaly in another control.

    ASCX
    <script language="javascript">
    function mkdnLoss(){
    var val1 = document.getElementById('txtPrice').value;
    var val2 = document.getElementById('txtSoldPrice').value;
    var lossamt = val1 - val2;
    document.getElementById('txtLossAmount').value = lossamt;
    }
    </script>

    .VB
    txtSoldPrice.Attributes.Add("OnChange", "mkdnLoss();")

    Help is appreciated!

  • Re: Easy JS Question

    01-04-2006, 12:44 PM
    • Participant
      1,215 point Participant
    • timrolands
    • Member since 02-15-2004, 8:59 PM
    • Wisconsin, USA
    • Posts 245

    Could it just be the case? Try....

    txtSoldPrice.Attributes.Add("onChange", "mkdnLoss();")

     

  • Re: Easy JS Question

    01-05-2006, 11:21 AM
    • Participant
      1,430 point Participant
    • ech01
    • Member since 09-08-2004, 3:19 PM
    • MN
    • Posts 288

    Didn't work.

    I also have tried writing the script in the page load like so

    Dim js As New System.text.StringBuilder

    js.Append("<script type = 'text/javascript'>")

    js.Append("function mkdnLoss(){var val1 = document.getElementById('txtPrice').value;var val2 = document.getElementById('txtSoldPrice').value;var amt = val1 - val2;document.getElementById('txtLossAmount').value = amt;}")

    js.Append("</script>")

    Response.Write(js.ToString())

    I don't think the getElementById is working. Any other hints???

  • Re: Easy JS Question

    01-05-2006, 2:09 PM
    • Star
      9,585 point Star
    • cniknet
    • Member since 07-23-2002, 7:19 PM
    • Washington, DC
    • Posts 1,915

    The client-side ID of a control is not the same as the server-side ID. When referring to a control in JS, you need to use its ClientID property. For example, in your ASCX you could use:

    var myObject = document.getElementById("<%= txtPrice.ClientID %>");

    This will give you the handle to the object as expected.

    Nik

     

    Nik Kalyani
    Speerio, Inc.

    [DotNetNuke and ASP.Net solutions here]
  • Re: Easy JS Question

    01-06-2006, 9:49 AM
    • Participant
      1,430 point Participant
    • ech01
    • Member since 09-08-2004, 3:19 PM
    • MN
    • Posts 288
    Thanks that helped!
  • Re: Easy JS Question

    01-17-2006, 3:30 PM
    • Member
      585 point Member
    • sconard
    • Member since 12-02-2003, 9:31 AM
    • Posts 136

    I cannot set the value of the textbox with this assignment?

    function setGenSecMan(varGenSecMan){ 
       document.getElementById("").text = varGenSecMan; 
    }

  • Re: Easy JS Question

    01-17-2006, 6:47 PM
    • Star
      9,585 point Star
    • cniknet
    • Member since 07-23-2002, 7:19 PM
    • Washington, DC
    • Posts 1,915

    No. The document.getElementById() method requires a parameter which is the ID of an element on the page.

    Nik

    Nik Kalyani
    Speerio, Inc.

    [DotNetNuke and ASP.Net solutions here]
  • Re: Easy JS Question

    01-18-2006, 7:08 AM
    • Member
      585 point Member
    • sconard
    • Member since 12-02-2003, 9:31 AM
    • Posts 136

    document.getElementById("<%= tbHidGenSec.ClientID %>").text = varGenSecMan;

    When I copied and pasted from Visual Studio the delimited section was lost.  The above is the actual string.  I can also view the value within the function after I set it but it is either lost on postback or the value is never actually assigned to the control. 

    alert(document.getElementById("<%= tbHidGenSec.ClientID %>").text); //value of 2 as assigned in function

    Neither code behind or page show new value after postback

    Dim strVar As String = tbHidGenSec.Text

Page 1 of 1 (8 items)