Be a Supporter!
GeneralJ
GeneralJ
  • Member since: Jul. 8, 2009
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
More JS Troubles 2012-04-04 20:16:35 Reply

For anybody that knows js absolutely refuses to work for me... here are some snippets if someone would kindly debug them.

$("#ssub").click(function() {
					var myForm = document.getElementByID('shrimp'); 
					var tag = myForm.getElementsByTagName('select'); 
					var count = 0; 
				
					for (var i = 0, len = tag.length; i < len; i++) {
						count++
					}

					alert(count); 
				})

... and the corresponding HTML:

<form id="shrimp">
									<div id="sblc">
										<div class="opt">
											<select>
												<option>Domestic</option>
												<option>Tiger</option>
												<option>Peeled Raw</option>
												<option>Cooked Peeled Tail On<option>
											</select>
											<select> 
												<option>1 lbs</option>
												<option>2 lbs</option>
												<option>3 lbs</option>
											</select>
										</div>
									</div>
									<div>
									<button id="snew" type="button">Add Order</button>
									<button id="ssub" type="button">Enter Order</button>
									</div>
								</form>
							</div>

Thank you for any assistance.
I need a break now :/

GeneralJ
GeneralJ
  • Member since: Jul. 8, 2009
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to More JS Troubles 2012-04-04 20:22:06 Reply

Since I was oh so clear... I'm attempting to find the number of select boxes in the given form. i want to cant them and throw the data to a larger form.
Sorry about that.

pirateplatypus
pirateplatypus
  • Member since: Sep. 27, 2011
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to More JS Troubles 2012-04-04 20:28:31 Reply

The only thing I can seem to see is that

for (var i = 0<strong>,</strong> len = tag.length; i < len; i++) {

should be

for (var i = 0<strong>;</strong> len = tag.length; i < len; i++) {

This BBS uses the code tags right?


"If loving Python is crazy then I don't want to be sane." -Diki

pirateplatypus
pirateplatypus
  • Member since: Sep. 27, 2011
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to More JS Troubles 2012-04-04 20:30:10 Reply

At 4/4/12 08:28 PM, pirateplatypus wrote:

D'oh! the strong tag doesn't seem to work when nested in a code tag. The problem I was trying to suggest was that in your for loop from the JS you used a comma instead of a semi-colon after declaring i.


"If loving Python is crazy then I don't want to be sane." -Diki

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to More JS Troubles 2012-04-04 20:32:12 Reply

Well, first off, why are you mixing jQuery with document.getElementById?

However to do what you want is trivial:

var num = jQuery('.opt select').length;

You can test it out here.

Doing jQuery('.opt select') will return a jQuery object of all <select> containers inside a container with a class of "opt" (which is the <div> that you wrapped your <select> containers in).
Then the length property does what it implies; returns the number of objects found.

Note: You should give that <div> an id to reference with instead of a class; it's much faster.

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to More JS Troubles 2012-04-04 20:34:58 Reply

At 4/4/12 08:28 PM, pirateplatypus wrote: The only thing I can seem to see is that

for (var i = 0, len = tag.length; i < len; i++) {

should be

for (var i = 0; len = tag.length; i < len; i++) {

This BBS uses the code tags right?

There is nothing wrong with his for loop declaration. It is valid to define more than one variable in a for loop.
Replacing the comma with a semi-colon would actually invalidate the JavaScript and cause errors.

pirateplatypus
pirateplatypus
  • Member since: Sep. 27, 2011
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to More JS Troubles 2012-04-04 20:54:37 Reply

At 4/4/12 08:34 PM, Diki wrote:
There is nothing wrong with his for loop declaration. It is valid to define more than one variable in a for loop.
Replacing the comma with a semi-colon would actually invalidate the JavaScript and cause errors.

D'oh! Thanks for pointing that out. I didn't notice the second variable in there.


"If loving Python is crazy then I don't want to be sane." -Diki

GeneralJ
GeneralJ
  • Member since: Jul. 8, 2009
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to More JS Troubles 2012-04-05 00:35:04 Reply

At 4/4/12 08:32 PM, Diki wrote: Well, first off, why are you mixing jQuery with document.getElementById?

However to do what you want is trivial:

var num = jQuery('.opt select').length;

You can test it out here.

Doing jQuery('.opt select') will return a jQuery object of all <select> containers inside a container with a class of "opt" (which is the <div> that you wrapped your <select> containers in).
Then the length property does what it implies; returns the number of objects found.

Note: You should give that <div> an id to reference with instead of a class; it's much faster.

The reason I did it like that is because there will be multiple forms (this being one of them) that will feed to a master form to allocate data in a single $_POST. This is why I attempted to tag a form id. Would the following work:

var num = jQuery('#shrimp select').length;

Also, is the fact that I'm weaving raw JS with jQuery actually be my problem?

Thanks for the responses.