Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    May 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    noob needs help!

    I konw this isnt right - but I don't know how to write an else statement.
    I'm trying to use a radio button - to populate another field.
    The code doesnt work because regardless of which button I select - the price displayed is $40 - which is the LAST item!

    html code:
    <tr><td>Item:</td><td>
    <input type="radio" name="item" value="Vest" onClick="addPrice()" /> Vest<br />
    <input type="radio" name="item" value="Shorts" onClick="addPrice()" /> Shorts<br />
    <input type="radio" name="item" value="LSJersey" /> L/S Jersey<br />
    <input type="radio" name="item" value="SSJersey" /> S/S Jersey<br />
    </td>
    </tr>


    The javascript to populate the $ field:

    function addPrice(){
    if(webform.item.value="Vest")
    {
    webform.amount.value="95";
    document.getElementById("amount").focus();
    }
    }

    function addPrice(){
    if(webform.item.value="Shorts")
    {
    webform.amount.value="50";
    document.getElementById("amount");
    }
    }
    function addPrice(){
    if(webform.item.value="LSJersey")
    {
    webform.amount.value="60";
    document.getElementById("amount");
    }
    }
    function addPrice(){
    if(webform.item.value="SSJersey")
    {
    webform.amount.value="40";
    document.getElementById("amount");
    }
    }
    function addPrice(){
    if(webform.item.value="SSJersey")
    {
    webform.amount.value="40";
    document.getElementById("amount");
    }
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Several major problems.

    webform not defined.
    Five functions all named addPrice.

    You should look at the way to access the values of radio buttons:-

    Code:
    <input type="radio" name="item" value="95" onClick="addPrice()" /> Vest<br />
    <input type="radio" name="item" value="60" onClick="addPrice()" /> Shorts<br />
    <input type="radio" name="item" value="50" /> L/S Jersey<br />
    <input type="radio" name="item" value="40" /> S/S Jersey<br />
    
    <script type = "text/javascript">
    function addPrice() {
    for (var i = 0; i < document.webform.item.length; i++) {
    if (document.webform.item[i]. checked) {
    amount = document.webform.item[i].value;
    }
    }
    </script>
    Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose.

  • #3
    New to the CF scene
    Join Date
    May 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I apologize about the title. I read the rules, but apparently missed that line...

    The code makes sense to me about defining the values in the item field. However - the $ field is not populating at all now.

    I moved the script code within my form (I had it in the <head> before).
    IE gives me an error telling me that an object is expected at the line in my form.

    I have now changed my code to read:
    <tr><td>Item:</td><td>
    <input type="radio" name="item" value="95" onClick="addPrice()" /> Vest<br />
    <input type="radio" name="item" value="50" onClick="addPrice()" /> Shorts<br />
    <input type="radio" name="item" value="60" onClick="addPrice()"/> L/S Jersey<br />
    <input type="radio" name="item" value="40" onClick="addPrice()"/> S/S Jersey<br />
    </td>
    <script type = "text/javascript">
    function addPrice(){
    <!--alert ("hi");-->
    for (var i = 0; i < document.webform.item.length; i++) {
    if (document.webform.item[i]. checked) {
    amount = document.webform.item[i].value;
    }
    }
    </script>
    </tr>
    <tr>
    <td>Amount</td><td><input name="amount" id="amount" size="7" /></td>
    </tr>

  • #4
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    You haven't closed the script yet:
    Code:
    <script type = "text/javascript">
    function addPrice(){
    <!--alert ("hi");-->
    for (var i = 0; i < document.webform.item.length; i++) {
    if (document.webform.item[i]. checked) {
    amount = document.webform.item[i].value;
    }
    }
    }
    </script>
    Maybe you haven't created a form naming webform, This would work:
    Code:
     <form name="webform">
     <table>
     <tr><td>Item:</td><td>
    <input type="radio" name="item" value="95" onClick="addPrice()" /> Vest<br />
    <input type="radio" name="item" value="50" onClick="addPrice()" /> Shorts<br />
    <input type="radio" name="item" value="60" onClick="addPrice()"/> L/S Jersey<br />
    <input type="radio" name="item" value="40" onClick="addPrice()"/> S/S Jersey<br />
    </td>
    <script type = "text/javascript">
    function addPrice(){
    <!--alert ("hi");-->
    for (var i = 0; i < document.webform.item.length; i++) {
    if (document.webform.item[i]. checked) {
    amount = document.webform.item[i].value;
    alert(amount);
    }
    }
    }
    </script> 
    </tr>
    <tr>
    <td>Amount</td><td><input name="amount" id="amount" size="7" /></td>
    </tr>
    </table>
    </form>
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    and BTW, I request to read forum rules regarding subject of a thread, http://www.codingforums.com/postguide.htm
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by abduraooft View Post
    and BTW, I request to read forum rules regarding subject of a thread, http://www.codingforums.com/postguide.htm
    That point has already been made, and the OP has apologised.

  • #7
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Quote Originally Posted by Philip M View Post
    That point has already been made, and the OP has apologised.
    Oh.. I haven't seen that apology, but I think he could still edit his original post to change the title, rather than just apologising
    Last edited by abduraooft; 05-03-2008 at 09:33 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by abduraooft View Post
    Oh.. I haven't seen that apology, but I think he could still edit his original post to change the title, rather than just apologising
    Stop hitting the guy!
    He is a new coder and perhaps has not realised that he can edit the title.

  • #9
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Sorry Philip. I'm not hitting anyone. I hadn't really found your notification about the title and the OP's response... (my badness). So, when I realised the fact, I just pointed out the edit feature. Hope luv2pdl has got his solution.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

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