dcsimg
www.webdeveloper.com
Results 1 to 5 of 5

Thread: Javascript code not firing

  1. #1
    Join Date
    Sep 2017
    Posts
    7

    Javascript code not firing

    Hi guys

    I am totally new to .net and need to update a page that was created by someone else.

    Basically, I need the following to work:

    ----------------------------------
    <aspropDownList ID="minprice" runat="server" class="form-control" onchange="sum();">
    <asp:ListItem Text="0" Value="0" />
    <asp:ListItem Text="500" Value="500" />
    <asp:ListItem Text="1,000" Value="1000" />
    <asp:ListItem Text="2,000" Value="2000" />
    <asp:ListItem Text="3,000" Value="3000" />
    </aspropDownList>

    <aspropDownList ID="maxprice" runat="server" class="form-control" onchange="sum();"
    <asp:ListItem Text="500" Value="500" />
    <asp:ListItem Text="1,000" Value="1000" />
    <asp:ListItem Text="2,000" Value="2000" />
    <asp:ListItem Text="3,000" Value="3000" />
    </aspropDownList>


    <asp:TextBox ID="newprice" runat="server" class="form-control" />


    <script>
    function sum() {
    var txtFirstNumberValue = document.getElementById('minprice').value;
    var txtSecondNumberValue = document.getElementById('maxprice').value;
    var result = parseInt(txtFirstNumberValue) + parseInt(txtSecondNumberValue);
    if (!isNaN(result)) {
    document.getElementById('newprice').value = result;
    }
    }
    </script>
    ----------------------------------

    What should happen is that the "newprice" textbox should populate with the total of the "minprice" and "maxprice" fields. I'm just clutching at straws here.

    Any help would be fully appreciated

    Best regards

    Rod from the UK

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,425
    When you post code, please use the forum BBCode tags and wrap them around the code you post, it stops smilie faces popping up as well as keeps formatting and depending on what code you have, some syntax highlight.

    Your page must fully load before the DOM elements are made available to JavaScript.

    You are also triggering the function after the one element is changed and that is likely breaking the script for you because theres no checking to see if an element is selected or not. I suggest you add and check for an element that is listed as "Please Select" and give it a value of -1 or 0 and tell the browser that it is to be set as the selected item, so when the item is changed, you can test if anything has been selected and update if needed.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  3. #3
    Join Date
    Sep 2017
    Posts
    7
    Hi Super Moderator

    Thank you so much for your reply.

    I was worried it was going to be something like this. Unfortunately, I have no idea about .net. If it was classic asp I would have half a chance but our developer has left us in limbo so I'm trying to piece our website back together with next to no knowledge of .net.

    Would it be possible to provide some example script that I can follow? If so, then that would be hugely appreciated.

    I look forward to hearing from you

    Best regards

    Rod from the UK

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,425
    You will find lots of examples of how to get information from a dropdown list as well as populating a drop down, it is a well covered subject.

    Go to the JavaScript forum and do a search (not in the search box) in the forum search and look up dropdown menu.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  5. #5
    Join Date
    Sep 2017
    Posts
    7
    Thanks, much appreciated

    Best regards

    Rod from the UK

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center