title: Select a HTML Selection field based on a JavaScript Variable Value?
body: <p>I am editing an existing Smarty Template that has JavaScript like below which builds a portion of the page.  </p>
  
  <p>In the code snippet of JavaScript below you can see a <strong>Value</strong> for an HTML Input field is set with this JavaScript variable <code>var priority</code></p>
  
  <pre><code>var priority = 'Urgent';
  
  e.innerHTML += '&lt;input name="priority_'+num+'" id="priority_'+num+'" size=6 type="text" value="'+priority+'" class="priority"&gt;';
  </code></pre>
  
  <p>In my template file there is many different variables and sections very similar to this one posted above.  if i can get help with this one, then i can apply the changes across all my other ones so there is no need to post them all here.</p>
  
  <p>What I need to do is replace this text input field with a Dropdown Selection field and then also have the correct value to be selected based on the value of the <code>var priority</code> variable.</p>
  
  <p>So the new field would look something like this instead of the old text input field shown above...</p>
  
  <pre><code>e.innerHTML += '&lt;select name="priority_'+num+'" id="priority_'+num+'"&gt;
    &lt;option value="Low"&gt;Low&lt;/option&gt;
    &lt;option value="Medium"&gt;Medium&lt;/option&gt;
    &lt;option value="High"&gt;High&lt;/option&gt;
    &lt;option value="Urgent"&gt;Urgent&lt;/option&gt;
  &lt;/select&gt;';
  </code></pre>
  
  <p>But I would then need to have the value of this JavaScript variable <code>var priority</code> to have the correct selection value <strong>SELECTED</strong></p>
  
  <p>I would appreciate any help I can get with this please?</p>
  
  <hr>
  
  <hr>
  
  <p><strong>UPDATES</strong></p>
  
  <p>After trying out a couple ideas from the comments I have realized I need to go ahead an post more of my code and explain more as it seems to be a bit more complex than I had thought originally.</p>
  
  <p>So below is a JavaScript function that gets called which inserts a Row of HTML form inputs and selections each time the Function is called.  It not only inserts this row into the DOM but if currecnt values are supplied, it then populates the Values for each field as well.  The problem is it is currently only text input fields and I am trying to convert a few of the text fields into Selection fields.</p>
  
  <pre><code>&lt;script type='text/javascript'&gt;
  
  var project_tasks = new Array();
  var priotities = ['Low', 'Medium', 'Urgent'];
  
  
  YAHOO.util.Event.onDOMReady(function(){
                  // code iterates my PHP and calls this for each existing Tasks row to add and
                  // Load in all the existing Tasks for this record.
                  add_task_row("","fgsdgsdfjhg","gsdgsdfhjgh","hsdgsdfj","Low","klk");
                  add_task_row("","sdgsdfgdfg","dfgsdfgsdfg","dfgdsfg","Urgent","hlf");
  
  });
  
  
  function add_task_row(taskid,name,description,status,priority,type){
      var ii = document.getElementById("project_tasks");
      var num = document.getElementById("tasks_count").value;
      num++;
  
  
      var e = document.createElement("div");
      e.setAttribute('id','task_'+num);
  
      //e.innerHTML =  '&lt;input name="taskID_'+num+'" id="taskID_'+num+'" size=0 type="hidden" value="'+taskID+'"&gt;';
  
      // Add an ID
      e.innerHTML =  '&lt;div style="float: left; width: 111px;"&gt;&lt;input name="taskid_'+num+'" id="taskid_'+num+'" size=9 type="text" value="'+taskid+'"&gt;&lt;/div&gt;';
  
      e.innerHTML +=  '&lt;div style="float: left; width: 400px;"&gt;&lt;input name="name_'+num+'" id="name_'+num+'" size=45 type="text" value="'+name+'"&gt;&lt;/div&gt;';
      e.innerHTML += '&lt;div style="float: left; width:  400px;"&gt;&lt;input name="description_'+num+'" id="description_'+num+'" size=45 type="text" value="'+description+'"&gt;&lt;/div&gt;';
      e.innerHTML += '&lt;div style="float: left; width:  90px;"&gt;&lt;input name="status_'+num+'" id="status_'+num+'" size=5 type="text" value="'+status+'"&gt;&lt;/div&gt;';
      //e.innerHTML += '&lt;div style="float: left; width:  90px;"&gt;&lt;input name="priority_'+num+'" id="priority_'+num+'" size=6 type="text" value="'+priority+'" class="priority"&gt;&lt;/div&gt;';
      e.innerHTML += '&lt;div style="float: left; width:  90px;"&gt;&lt;select name="priority_'+num+'" id="priority_'+num+'" class="priority"&gt;&lt;option value="Low"&gt;Low&lt;/option&gt;&lt;option value="Medium"&gt;Medium&lt;/option&gt;&lt;option value="High"&gt;High&lt;/option&gt;&lt;option value="Urgent"&gt;Urgent&lt;/option&gt;&lt;/select&gt;&lt;/div&gt;';
      e.innerHTML += '&lt;div style="float: left; width:  90px;"&gt;&lt;input name="type_'+num+'" id="type_'+num+'" size=4 maxlength=6 type="text" value="'+type+'"&gt;&lt;/div&gt;';
      e.innerHTML += '&lt;div style="float: left; width:  30px;"&gt;&lt;button type="button" onclick="remove_item_row('+num+')"&gt;&lt;img src="index.php?entryPoint=getImage&amp;imageName=id-ff-clear.png"&gt;&lt;/button&gt;&lt;/div&gt;';
      e.innerHTML += '&lt;br style="clear:both;"&gt;';
  
      document.getElementById("tasks_count").value = num;
      ii.appendChild(e);
  
  
  }
  </code></pre>
  
  <p>What this function does above, is when I click a button to add a new Task, it calls this function and leaves each Function Variable empty which results in a new Task row inserted into teh DOM and each input is empty and ready for me to type in new values.</p>
  
  <p>Now when the page loads, it first iterates over an array of values and call this function for each Tasks in the array, it then sets the Value for each inputs based on the value in the Tasks array.</p>
  
  <p>So you can see this same code works for adding new Tasks rows and editing existing Tasks rows.</p>
  
  <p>I just need to be able to convert some of the text input fields into selection fields and then still be able to set the value for them on existing Tasks records that are loaded.</p>
  
  <p>Hopefully this makes some sort of sense.  Thanks for all help!</p>