counting

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
naveendk.55
Forum Newbie
Posts: 24
Joined: Tue Aug 16, 2011 10:13 am

counting

Post by naveendk.55 »

Hi, I'm new to javascripting. I need help with below code.

There are six html <select> tags. Each tag has options YES, NO and NA. I want to count the number of 'NO' selected by the user and assign it to a textbox value. help please.
KCAstroTech
Forum Commoner
Posts: 33
Joined: Sat Aug 27, 2011 11:16 pm

Re: counting

Post by KCAstroTech »

If you are using jQuery (http://www.jquery.com) then you could use:

Code: Select all

$("select[value='NO']")
It is a bit vague but that should work. Essentially you are telling jQuery to look at ALL select tags and find only the ones with the value of "NO" presuming of course that the select looks something like:

Code: Select all

<select>
<option value="YES">YES</option>
<option value="NO">NO</option>
<option value="NA">NA</option>
</select
Otherwise you have to use some sort of loop like:

Code: Select all

//Create a variable called selects then assign it an array of the all of select elements in the document
var selects = document.getElementsByTagName('select');
//Loop through the selects
for(var i in selects){
  //Check if the select's value equals "NO"
  if(selects[i].value == 'NO'){
    //If the select's value is no then do something here
  }
}
Post Reply