Solved

UPDATE CHECKBOX VALUE

Posted on 2008-10-13
17
340 Views
Last Modified: 2012-05-05
is there any way where we can say.

if(document.formbask[i].value == '0')
{
go back to previous cell (i -1)
alert('you have zero quantity for that line item, removing item from basket')
set that cell of document.formbask.dgrp.checked == true
}
** Question moved from Web Applications zone to Javascript zone - b0lsc0tt [ZAPE] **

Open in new window

0
Comment
Question by:tampa_vinnie
  • 10
  • 7
17 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
Is formbask still the name of the form?  If so then what actually do you want to check for a value?  The form itself won't have one but that part is probably just a typo?  What type of field is it that you are checking?
What is the previous cell?  I assume you mean a previous form field?  What type of form element?  It seems like it may be a checkbox but that isn't completely clear.
A key to this is the name of the different form elements.  Please provide the html for that part of the page or a description of the type and name for each form element.  Javascript can look for a value and then modify the value of another element but the specific script depends on the way the form is set up, its fields and their type and name.
Let me know if you have a question about what I have said or need.
bol
p.s.  I modified the zone this was in.  Instead of Web Applications it should be in the Javascript zone.  If you want both or some other change then let me know.  However the Javascript zone is a key since that is what this is about.  As a zone advisor I was able to make the change for you but let me know if you have a question.
0
 

Author Comment

by:tampa_vinnie
Comment Utility
attached is a picture and here is the html. let's start with how to update the checkbox value on a single record.

what we need to do is.

** code moved to snippet - b0lsc0tt [ZAPE] **

if (the textbox value is zero)

{

go one cell to the left and check the check box for 'remove' <--- this is the code I need

}
 

html to match the picture:

<form name=formbask id="formbask" action="go.asp" method="POST">

<input type=hidden name="prog" value="viewbask">

<input type=hidden name="pend" value="">

<input type=hidden name="lastprog" value="viewbask">

<input type=hidden name="pendovr" value="">
 

<table cellpadding=2 cellspacing=0 border=1 width=100% class=chkout>
 

<tr>
 

<th align=center>Remove</th>
 

<th align=center>Qty</th>
 

<th align=center>Availability</th>
 

<th>Product#</th>
 

<th>Description</th>
 

<th align=right>Unit Price</th>
 

<th align=right>Total</th>
 

</tr>
 
 
 
 
 
 
 

<RxP SQ,SELECT (QtyOnHand-QtyCommit) AS QtyAvail FROM SStok WHERE StokID='6682-98-04' AND SLocID='001'></RP>
 

<RxP XQ,s></RP>
 

<input type=hidden name=qgrp value="412854">

<input type=hidden name=qgrp2 value="412854">
 

<tr bgcolor=#C1CDCD>

<td align=center>

<input class=chkbox type=checkbox name=dgrp value="412854">

</td>

<td align=center>
 

<input class=inpC3 type=text name=qty value="1">

</td>

<td align=center>
 
 

<img src="custom/images/yes_stock.gif">

</td>

<td>

6682-98-04

</td>

<td>
 

Sierra 3 Pocket Pants
 
 
 
 

<br>Color: Chocolate Kiss / Size: L

</td>

<td align=right>

13.99

</td>

<td align=right>

$13.99

</td>

</tr> 
 
 

</td>
 

</tr>
 

</table>

Open in new window

cart.png
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
For the html you showed you would use the script in the snippet below.  If you have more than one Remove checkbox and Qty input then they should have different names, especially the Qty fields.  If that is the case then the code below will probably need to change some.  Provide details if this is the case but I will assume the page only has the one.
bol
p.s.  Notice the 2 parts of the code.  One if the javascript and goes in the head section of the page.  The other is a change to your input field to add the event to call the javascript.

JAVASCRIPT CODE IN HEAD SECTION

============================

function chkQty(ele) {

  if (ele.value == 0) {

    document.formbask.dgrp.checked = true;

  }

}

=========================

HTML CODE IN QTY FIELD

=========================

    <input class=inpC3 type=text name=qty value="1" onblur="chkQty(this);">

Open in new window

0
 

Author Comment

by:tampa_vinnie
Comment Utility
B0l, you are right,

this works great for one item, but for more then one it doesn't.

unfortunately, the names of the elements (qty & dgrp) are the same if repeated.

is there any way to still make this work if this is the case?
0
 

Author Comment

by:tampa_vinnie
Comment Utility
ok, i was able to change the way the html is rendered to have a qty id field be unique in every record like so. does this help?

example:
---------
    <td align=center>
      <input class=chkbox type=checkbox name="dgrp" value="236114">
    </td>

    <td align=center>
      <input class=inpC3 type=text name="qty" id="qty1" onblur="chkQty(this);" value="3">
    </td>
-----------
    <td align=center>
      <input class=chkbox type=checkbox name="dgrp" value="236114">
    </td>

    <td align=center>
      <input class=inpC3 type=text name="qty" id="qty2" onblur="chkQty(this);" value="3">
    </td>
-----------
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
That does help.  What about the dgrp field (the checkbox)?  Can you make that dgrp1, dgrp2, etc?  If not is the value really the same in each?  In a case like this I'd rather see the name be unique but you definitely need one of them to be unique.
There can be a way for Javascript to do it even with the names and values the same but your form will have invalid html and browsers will have issues with it.  It should be corrected.  Changing the Qty was a good start towards getting valid html and a good form.
Did you mean to leave the name value the same in those Qty fields?  The ID is unique, which has to be the case, but the name is still the same.  For text inputs those should also be different.  If something makes the html then there is always a way to make some change so that script creates unique inputs even when a script is used.  Let me know if you want some suggestions on that but I would need more info on how it is done now.
Let me know about the name in the Qty and the checkboxes and I can show you how to change the code so it will work with multiple rows of those fields.
bol
0
 

Author Comment

by:tampa_vinnie
Comment Utility
i can add an id field for dgrp:

example:
---------
    <td align=center>
      <input class=chkbox type=checkbox name="dgrp" id="dgrp1"value="236114">
    </td>

    <td align=center>
      <input class=inpC3 type=text name="qty" id="qty1" onblur="chkQty(this);" value="3">
    </td>
-----------
    <td align=center>
      <input class=chkbox type=checkbox name="dgrp" id="dgrp2" value="236114">
    </td>

    <td align=center>
      <input class=inpC3 type=text name="qty" id="qty2" onblur="chkQty(this);" value="3">
    </td>
-------------

it's possible for the code to start with dgrp5,6,7,8 that type of thing based on different scenarios, it's hard to know what number it will start at, but it will increment sequentially.

is there javascript code to accomodate this?
0
 

Author Comment

by:tampa_vinnie
Comment Utility
i'm sorry, but i'm unable to change the name to be unique without giving us causing other features to fail.


 is there code to accomodate us like the example for now?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 500 total points
Comment Utility
If the ID is unique then we can modify that function a bit and should be able to do it.  The shared name for the text inputs (the Qty fields) could still cause other problems but won't stop us for this part.
Try the new function in the snippet below.  Let me know if you have a question or how it works.  The key to it is the common number between the checkbox and related Qty field.  As long as it is the same number used in the id the script should work for all rows.
bol

function chkQty(ele) {

  if (ele.value == 0) {

    var theId = ele.id.match(/\d+/);

    document.getElementById('dgrp' + theId).checked = true;

  }

}

Open in new window

0
 

Author Comment

by:tampa_vinnie
Comment Utility
phenominal, thank you!
0
 

Author Comment

by:tampa_vinnie
Comment Utility
out of curiousity, how does this part work: /\d+/
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
Your welcome!  I'm glad I could help and thanks for the fun question, the grade and the points.
The part you ask about is a regular expression.  The slashes (/) are used to designate and surround the expression.  \d is shorthand for a "character class".  It is the same as 0-9 and will match any digit.  The + character is a quantifier and means 1 or more characters (as many as possible).  Used with javascript's match() function it looks in the id for numbers and returns that match.  Basically I use it to grab the number from the id so I can use it in the other element, which will use the same number.
Let me know if you have a question about that explanation.
bol
0
 

Author Comment

by:tampa_vinnie
Comment Utility
any ideas on how to make the javascrip work if the id values start w/ blank

i.e. id = qty, qty1, qty2, etc...
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
My suggestion is to use a number for everyone so there is consistency.
However a minor change should make the function handle that lack of a number.  Change the 3rd line (in the snippet above) to ...
    var theId = (ele.id.match(/\d+/) == null)? "" : ele.id.match(/\d+/);
Let me know how that helps or if you have a question.
bol
0
 

Author Comment

by:tampa_vinnie
Comment Utility
thanks again for this.

much appreciated this works great.
0
 

Author Comment

by:tampa_vinnie
Comment Utility
does the  : mean 'or' meaning the id can equal this OR that

also what is the purpose of the ?"" part?
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
The change I made with a ternary operator.  It is basically an If/Else.  The ? is used to show the end of the If part.  The : is used to break what is done if the If is true from what is done if not (i.e. the Else part).
I hope that is clear. :)  There are some other good details at http://www.mredkj.com/tutorials/reference_js_ternary.html but you could also open a new question here if you want more info, although I am not real sure what more to add. :)
bol
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now