0

I have a form with radiobox, input (type='text' or type='checkbox'), textarea... dropdown menu (select).. and I want to show a different div depending on how much input are filled/checked. I wrote this for the input (type='text') selecting input id by id but how it is possible do the same thing for select and textarea

<script type="text/javascript">



$(document).ready(function() {

$("input[id*='ebit'], input[id*='nome_progetto'], input[id*='posizione_netta'], input[id*='descrizione'], input[id*='stato_transazione']").blur(function() {
   var counter=0;
    $("input[id*='ebit'], input[id*='posizione_netta']").each(function(ind, val){
        if($(val).val().trim()!==""){
            counter=counter+2;
        }
    });
     $("input[id*='nome_progetto'], input[id*='descrizione']").each(function(ind, val){
        if($(val).val().trim()!==""){
            counter++;
        }
    });

     $("input[id*='stato_transazione']").each(function(ind, val){
        if($(val).val().trim()!==""){
            counter++;
        }
    });


    $("#green_0, #green_1, #green_2, #green_3, #green_4, #green_5, #green_6, #green_7, #green_8, #green_9, #green_10, #green_11, #green_12, #green_13, #green_14, #green_15").hide();

    $("#green_"+counter).show(); 
});
});
Bella
  • 139
  • 1
  • 2
  • 10

3 Answers3

1

If your textarea and select have ids, you can refer to their values like

select:

$("#selectId").val();

textarea:

$("#textAreaId").html();
Michael Sanchez
  • 1,215
  • 1
  • 11
  • 19
0

have a option

<option selected="selected" disabled="disabled" value="">Select Option</option>

and use

if($("#selectId").val()!=='')
 counter++;

And for textboxes you can use

if($("#textAreaId").val()!=='')
    counter++;
Ekansh Rastogi
  • 2,418
  • 2
  • 14
  • 23
  • this does not work, i have to add something at the signature of the function? such as option[id*='myid'] ? – Bella Aug 19 '14 at 12:37
0

If you are using jQuery

$('option:selected').text()

Gives you the selected option text.

$('option:selected').val()

Gives you selected option value

$('#myselect option:selected').val()

If you have multiple select boxes. Where myselect is your select ID

Jabran Saeed
  • 5,760
  • 3
  • 21
  • 37