Lanezeri
Lanezeri

Reputation: 1

JS/jQuery to display textboxes based on radio/select values

Basically, I'm trying to display 1 to 10 textboxes depending on how many the user selects, IF the user wants to send them to recipient emails.

I have a dropdown select with 1-10 choices/values, two radio buttons, and ten textboxes with supporting div's. I am using snips of code that the previous developer (whose place I took) used which I can only assume is jQuery, but I have some of the small Javascript knowledge I possess mixed in with it. Not really sure if I am allowed to do that or not. :/ Javascript/jQuery are my nemesis.

Here's what I have so far (live server): http://www.dealraiser.net/gift

Javascript/jQuery used:

<script type="text/javascript">
// SHOW HIDDEN TEXTBOXES
function updateDisplay(select) {
    // ACCESS RADIO OBJECT
    var radioValue = getRadioCheckedValue('sending');
    // SHOW FIELD IF RADIO IS SET TO 'R'
    if (radioValue == 'r') { showField(); }
}

// BORROWED FROM GOOGLE - EASIER TO USE PRE-EXISTING
function getRadioCheckedValue(radio_name) {
   var oRadio = document.forms[0].elements[radio_name];

   for(var i = 0; i < oRadio.length; i++) {
      if(oRadio[i].checked) {
         return oRadio[i].value;
      }
   }

   return '';
}
// DISPLAY FIELD, HIDE OTHERS
function showField() {
    // HIDE ALL EXTRA OPTIONS
    for (i=1, i<10, i++) {
        $("#re"+i).hide();;
        $('#r'+i).removeClass('required');
    }

    if (radio.value == 'r') {
        // DETERMINE NUMBER OF EMAILS TO SHOW
        var giftNumber = document.getElementById('giftnumber').value;

        // DISPLAY ONLY THE RIGHT AMOUNT OF EMAIL INPUTS
        for (i=1, i<=giftNumber, i++) {
            $("#re"+i).show();
            $('#r'+i).addClass('required');
        }
    }
}

// TOGGLE RADIO ON/OFF
function doToggle(radio) {
    // HIDE ALL EXTRA OPTIONS
    for (i=1, i<10,i++) {
        $("#re"+i).hide();
        $('#r'+i).removeClass('required');
    }

    if (radio.value == 'r') {
        // DETERMINE NUMBER OF EMAILS TO SHOW
        var giftNumber = document.getElementById('giftnumber').value;

        // DISPLAY ONLY THE RIGHT AMOUNT OF EMAIL INPUTS
        for (i=1, i<=giftNumber, i++) {
            $("#re"+i).show();
            $('#r'+i).addClass('required');
        }
    }
}

</script>

Here is the HTML:

<h3>1. How many subscriptions would you like to gift?   
    <select name="giftnumber" size="1" id="giftnumber" onChange="updateDisplay(this)" style="width: 48px">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="7">7</option>
      <option value="8">8</option>
      <option value="9">9</option>
      <option value="10">10</option>
  </select></h3>
  <h3>2. Sending options</h3>
  <p>
    <label>
      <input type="radio" name="sending" id="sending" value="b" onChange="doToggle(this)" />
      Send the pre-paid codes to my email and I will forward them.</label>
  </p>
  <p>
    <label>
      <input type="radio" name="sending" id="sending" value="r" onChange="doToggle(this)" />
      Send the pre-paid codes to the recipient email address(es).</label>
  </p>
  <div id="re1" name="re1" style="display:none;">
      <p>Recipient Email: <input name="r1" id="r1" type="text" /></p>
  </div>
  <div id="re2" name="re2" style="display:none;">
      <p>Recipient Email: <input name="r2" id="r2" type="text" /></p>
  </div>
  <div id="re3" name="re3" style="display:none;">
      <p>Recipient Email: <input name="r3" id="r3" type="text" /></p>
  </div>
  <div id="re4" name="re4" style="display:none;">
      <p>Recipient Email: <input name="r4" id="r4" type="text" /></p>
  </div>
  <div id="re5" name="re5" style="display:none;">
      <p>Recipient Email: <input name="r5" id="r5" type="text" /></p>
  </div>
  <div id="re6" name="re6" style="display:none;">
      <p>Recipient Email: <input name="r6" id="r6" type="text" /></p>
  </div>
  <div id="re7" name="re7" style="display:none;">
      <p>Recipient Email: <input name="r7" id="r7" type="text" /></p>
  </div>
  <div id="re8" name="re8" style="display:none;">
      <p>Recipient Email: <input name="r8" id="r8" type="text" /></p>
  </div>
  <div id="re9" name="re9" style="display:none;">
      <p>Recipient Email: <input name="r9" id="r9" type="text" /></p>
  </div>
  <div id="re10" name="re10" style="display:none;">
      <p>Recipient Email: <input name="r1" id="r10" type="text" /></p>
  </div>

Upvotes: 2

Views: 741

Answers (2)

sir_thursday
sir_thursday

Reputation: 5409

Use something like this:

function showtextareas(sel) {
    var value = sel.options[sel.selectedIndex].value;  
    if(value==1) { 
       //show one textarea 
    }
    else if(...) { ... }
}

<select onchange="showtextareas(this)">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
// ......
<option value="10">10</option>
</select>

BTW I dunno why color formatting is weird...

Upvotes: 0

Thiago Custodio
Thiago Custodio

Reputation: 18387

There is ';' missing at line 180

before:

for (i=1, i<10, i++)

after:

for (i=1; i<10; i++)

Upvotes: 2

Related Questions