user1490145
user1490145

Reputation: 153

How to insert numbers rather than option type into a textbox

I need helping appending values into a textbox. What happens is that with the relevant piece of code below, the user can add an "Option Type" from a table row into the textbox. For example if the user clicks on the "Add" button and within that row the Option Type is "A-D", it adds "A-D" in the textbox. But I don't want it to add "A-D" into the textbox. I want it to add a number with the textbox instead except for "True or False" and "Yes or No" options, they can be inserted as they are. The numbers for each option type is as follows:

Option Type             Number

A-C                     3
A-D                     4
A-E                     5
A-F                     6

...

A-Z                     26
True or False           True or False
Yes or No               Yes or No

My question is how can insert the numbers for all letter option types (True or False and Yes or No remain the same) into the textbox?

Below is the relevant code where it inserts the option type from the table row into the textbox:

Table row and add button:

      echo "<table border='1' id='resulttbl'>
      <tr>
      <th class='optiontypeth'>Option Type</th>
      </tr>";
      foreach ($searchResults as $key=>$question) {
        echo '<td class="optiontypetd">'.htmlspecialchars($searchOption[$key]).'</td>';
        echo "<td class='addtd'><button type='button' class='add' onclick=\"parent.addwindow('$searchOption[$key]');\">Add</button></td></tr>";
}
      echo "</table>";

Below is the textbox the option types are currently inserted into:

<input type="text" name="gridValues" class="gridTxt maxRow" id="mainGridTxt" readonly="readonly" />

Below is the function where it currently inserts the option type into the textbox above:

function addwindow(gridValues) { 

    if(window.console) console.log();

    if($(plusbutton_clicked).attr('id')=='mainPlusbutton') { 
        $('#mainGridTxt').val(gridValues);  
        } 

    $.modal.close(); 
    return false;
}

Upvotes: 1

Views: 208

Answers (1)

jeffjenx
jeffjenx

Reputation: 17457

Add a check before you change the value of the input field, something like:

if( questionText != "True or False" && questionText != "Yes or No" )
{
   // caution pseudo-code ahead:
   var myNumbers = {};
   myNumbers["A-C"] = "3";
   myNumbers["A-D"] = "4";
   // ...
   myNumbers["A-Z"] = "26";
   questionText = myNumbers[questionText];
}

$('#mainGridTxt').val(questionText);

There are lots of other ways to go about converting the questionText into the correct number, but I was simply showing the logic for you can accommodate non-T/F or -Y/N responses.

Upvotes: 1

Related Questions