SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Addict chartahir's Avatar
    Join Date
    Oct 2006
    Location
    Karachi, Pakistan
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Java sricpt problem

    I want to calculate the price, and if the user checks any checkbox then the value in the corresponding hidden text box is to be added and displayed on the same page before submitting the page,

    I do not want to use the hard coded names of text and check boxex because they are dynamically geneated and may differ in number.

    I'm generating the check box and text box name by concating the number at the end of string "chkMouse" (used for check box) and string "txtbox" (used for text box).

    When i'm displaying the name in alert it seems ok, but when trying to access the value of the text box using that name , it gives an error,

    The JS code is attached please help....

    Code:
    function ug() {
      
      var i = 1;
      var chkBox;
      var txtBox;
    
      while(i <= document.forms["test"].txtMouse.value) {
        chkBox = new String("chkMouse"+i);
    	txtBox = new String("txtMouse"+i);
    	
    	if(document.forms["test"].chkBox.checked)
    	  alert("Checked"+ document.forms["test"].txtBox.value);
    	else
    	 alert("UnChecked ");
    	 
    	i++;
      }
      
      //var grossValue = document.forms["test"].h_one.value;
    
       //alert(grossValue);
      
      //document.forms["test"].out_one.value = grossValue;
    }
    chartahir

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This won't work, because chkBox is a variable name not the NAME of an actual checkbox:
    document.forms["test"].chkBox.checked

    try
    document.forms["test"].elements[chkBox].checked

  3. #3
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another suggestion is to 'nix the special naming/hidden field altogether. Try looping through the elements 'un-named' - by using a different DOM technique - and making the value of the checkbox the amount you need:

    First, give your form a unique ID - let's just call it "priceForm" (code untested):
    Code:
    function ug() {
      var priceForm = document.getElementById("priceForm");
      var boxes = priceForm.getElementsByTagName("input"); //all input elements
      var currentAmount = parseInt(document.getElementById("gross").value);
      for (var i = 0; i < boxes.length; i++) {
        if(boxes[i].type == "checkbox" && boxes[i].checked) {
          //move away from float calculations and convert to cents first
          currentAmount = ((currentAmount * 100) + boxes[i].value)/100;
        }
      }
      document.getElementById("gross").value = currentAmount;
    }
    Your html can look something like this (keep in mind, the inline events should normally be avoided - but this is for illustration):
    HTML Code:
    <form id="priceForm">
    <input type="checkbox" name="nameofbox1" value="123">Check me for $1.23<br>
    <input type="checkbox" name="nameofbox2" value="529">Check me for $5.29<br>
    <input type="checkbox" name="nameofbox3" value="477">Check me for $4.77<br>
    ...
    <input type="button" onclick="ug();" value="Get Total"><br>
    <input type="text" id="gross">
    </form>

  4. #4
    SitePoint Addict chartahir's Avatar
    Join Date
    Oct 2006
    Location
    Karachi, Pakistan
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks for the help..

    Thanks for the help,i've solved this problem using AJAX and its working fine.
    chartahir


Bookmarks

Posting Permissions

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