Create fixed space (for image) after select element

Hi, I was wondering if it would be possible to add a little “correct” tick inside a dropdown menu to indicate that the correct answer has been selected. Or, alternatively, if it would be possible to allow a fixed space for “correct” ticks, so that the next question doesn’t get pushed to the right by the tick appearing, as in the code below.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">



   <title>English</title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  	<meta name="robots" content="index, follow"/>
   <meta name="description" content=""/>
<meta name="author" content="johnk" />

<meta name="keywords" content=""/>




    <style>
      ol li {
        background-image: none;
        padding:10px;
        display : inline;
      }
    </style>

  </head>
  <body>
    <div id="pagewrap">
      <div id="container">
        <div id="content">



	  				

          <h4>For, since or ago?</h4>
          <div class="scroll">
            <form method="post" id="myForm" name="f">
              <ol>
                <li>
               It's been raining
                  <select name="question1">
                    <option value="na">&nbsp;</option>
                    <option value="a"> for </option>
                    <option value="b"> since </option>
                      <option value="c"> ago </option>
                  </select>
                 2 hours.
                </li>
                <li>
                  They have lived here
                  <select name="question2">
                   <option value="na">&nbsp;</option>
                    <option value="a"> for </option>
                    <option value="b"> since </option>
                      <option value="c"> ago </option>
                  </select>
                 1990.
                </li>

              </ol>

             <input type="submit" id="submit" value="Result" />
              <input type="reset" class="reset" id="reset" name="reset" value="Start again" />
            </form>
             <div id="score"></div>
               <div class="address">

            </div>
          </div>
        </div>
      </div>
    </div>

    <script>
      Object.size = function(obj) {
        var size = 0, key;
        for (key in obj) {
          if (obj.hasOwnProperty(key)) size++;
        }
        return size;
      };

      (function(){
        "use strict";
        window.checkAnswers = function(opts){

          function validateInput(){
            var question,
                answer;

            for (question in opts) {
              if(opts.hasOwnProperty(question)) {
                answer = f.elements[question].options[f.elements[question].selectedIndex].value;
                if(answer === "na"){
                  opts[question].state = "not-filled-in";
                } else if(answer === opts[question].answer){
                  opts[question].state = "correct";
                } else {
                  opts[question].state = "error";
                }
              }
            }
          }

          function markCorrectOrIncorrect(){
            var question,
                li;

            for (question in opts) {
              if(opts.hasOwnProperty(question)) {
                var img = new Image(),
                li = f.elements[question].parentElement,
                feedbackImg = li.getElementsByTagName('img')[0];

                if (feedbackImg){
                  li.removeChild(feedbackImg);
                }

                if(opts[question].state === "correct"){
                  img.src = "http://www.littletherese.com/tick.jpg";
                  li.appendChild(img)
                } else if(opts[question].state === "error"){
                  img.src = "http://www.littletherese.com/x.jpg";
                  li.appendChild(img)
                }
              }
            }
          }

          function displayScore(){
            var correct = 0,
                error = 0,
                score = document.getElementById("score"),
                totalQuestions = Object.size(opts),
                question;

            for (question in opts) {
              if(opts.hasOwnProperty(question)) {
                if(opts[question].state === "correct"){
                  correct ++
                } else if(opts[question].state === "error"){
                  error ++
                }
              }
            }
            score.innerHTML = "You got " + correct + " out of " + totalQuestions;
          }

          function init(){
            validateInput();
            markCorrectOrIncorrect();
            displayScore();
          }

          init();
        }
      }());

      f.onsubmit = function(){
        checkAnswers({
          question1: {answer: "a"},
          question2: {answer: "b"},

        })
        return false;
      }

    f.reset.onclick = function(){
  var imgs = document.querySelectorAll('img');
  for(var i=0; i<imgs.length; i++){
    imgs[i].parentNode.removeChild(imgs[i]);
  }

  document.getElementById("score").innerHTML = "";
}

    </script>
  </body>
</html>

Thanks for any suggestions.

Hi,

Basically, no.
A standard select element either cannot be styled, or is limited in what can be styled.
Instead, you might want to try using a widget that looks and acts like a select box, but is made with HTML & Javascript.
For example: http://www.marghoobsuleman.com/jquery-image-dropdown

This is by far the easier option, but essentially a CSS question.
If you would prefer to go this route, let me know and I’ll move the question to the CSS forum.

Sounds like a good idea about the fixed space. But the jQuery dropdown looks nice too. I will have a look at it too.So, I suppose better to move to CSS forum then. Thanks.

Yeah, I would say try and get the fixed space thing working first, then implement the jQuery solution at a later date.
Thread moved.

ok, Thanks.

This seems to work for me, unless someone has a better solution?


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<style type="text/css">
 body { font-family:arial, helvetica, sans-serif; font-weight:bold; font-size:16px; color:#000; text-align:left; margin:3px 0px; }
 input { font-size:16px; width : 100px; padding-left : 4px;}
 #pagewrap  { width : auto; height:500px; margin-left:20px;  }
 #score { margin-top:10px; }
 ol li { padding:2px; 
      
       display: inline-block;
       } 
     .min_pc_1{
         width : 260px;
     }
      .min_pc_2{
         width : 290px;
     }
 
</style>
</head>

<body>

<div id="pagewrap">
<p>Type in the text boxes below</p>
  <form>
     <ol>
    <li class="min_pc_1">  He <input id="q1" type="text" size="auto" >  like football. </li>
    <li class="min_pc_2">  He <input id="q2" type="text" size="auto" > got any money. </li>
     
     </ol>
  
    <input type="submit" id="submit" value="Result">
    <input type="reset" class="reset" id="reset" name="reset" value="Start again">
  </form>
  <div id="score">
    <!-- writes to here -->
  </div>
</div>
<!-- end pagewrap -->
<script type="text/javascript">
 A= new Array();   // preload images
 A[0]=new Image(28,15); A[0].src="http://www.littletherese.com/tick.jpg";
 A[1]=new Image(22,15); A[1].src="http://www.littletherese.com/x.jpg";
//
$("form").on("submit", function(e){ e.preventDefault(); $("img").remove(); markAnswers(); });
// -----
$("input[type=reset]").on("click", function(){ $("img").remove(); $("#score").html(""); });
// ------
var answers = {
"q1":["doesn't", "doesn´t", "does not"], 
"q2":["hasn't","has not"] 
} 

var numQuestions=0, questionsCorrect=0; 
//
  function markAnswers()
    { $("input[type='text']").each( function(){
           // add one to questions counter on each pass
             numQuestions++;
             if($.inArray(this.value, answers[this.id]) == -1)
                  { $(this).parent().append('<img src="'+A[1].src+'">'); } 
             else { $(this).parent().append('<img src="'+A[0].src+'">'); 
                   // add one to correct answer counter on each pass
                    questionsCorrect++; }
            // display scores        
             $("#score").html("You got " + questionsCorrect + " out of " + numQuestions);     
        } );
   // reset counters after marking is complete   
      numQuestions=0; questionsCorrect=0;      
   } 
//                    
</script>
</div>

</body>

</html>

Thanks

Hi,

That seems to drop to two lines if there are two ticks. I would absolutely place the arrow at the end of the line and make space with some padding.

e.g.


ol li {
	padding:2px;
	display: inline-block;
	[B]padding-right:30px;
	position:relative;[/B]
}
[B]li img{position:absolute;right:0;top:50%;margin-top:-8px}[/B]
/*.min_pc_1 { width : 260px; }
.min_pc_2 { width : 290px; }
*/


No widths are needed.

In your earlier example with the select element you could have done similar and absolutely placed the arrow/cross over the end of the select although you would have needed smaller arrows. It’s probably better to have the arrow/cross outside the input/select though.

Ok, thanks. I’ll look more closely at it later!

That padding-right:30px; looks like an excellent solution, but why would the images being outside the input /select elements be better? And how would I go about doing it? Thanks!

Just in case the content in the control overlaps the image if for example you had more words in there. As long as you can manually control the data then you should be ok with the tick inside.

Something like this:


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>English</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="robots" content="index, follow"/>
<meta name="description" content=""/>
<meta name="author" content="johnk" />
<meta name="keywords" content=""/>
<style>
ol li {
	background-image: none;
	padding:10px;
	display : inline-block;
}
.check {
	display : inline-block;
	position:relative;
}
.check img {
	position:absolute;
	right:25px;
	top:50%;
	margin-top:-6px;
}
.scroll select { width:125px }
</style>
</head>
<body>
<div id="pagewrap">
		<div id="container">
				<div id="content">
						<h4>For, since or ago?</h4>
						<div class="scroll">
								<form method="post" id="myForm" name="f">
										<ol>
												<li> It's been raining <span class="check">
														<select name="question1">
																<option value="na">&nbsp;</option>
																<option value="a"> for </option>
																<option value="b"> since </option>
																<option value="c"> ago </option>
														</select>
														<img src="http://www.littletherese.com/tick.jpg" width="15" height="12"></span> 2 hours. </li>
												<li> They have lived here <span class="check">
														<select name="question2">
																<option value="na">&nbsp;</option>
																<option value="a"> for </option>
																<option value="b"> since </option>
																<option value="c"> ago </option>
														</select>
														<img src="http://www.littletherese.com/x.jpg" width="15" height="12"></span> 1990. </li>
										</ol>
										<input type="submit" id="submit" value="Result" />
										<input type="reset" class="reset" id="reset" name="reset" value="Start again" />
								</form>
								<div id="score"></div>
								<div class="address"> </div>
						</div>
				</div>
		</div>
</div>
<script>
      Object.size = function(obj) {
        var size = 0, key;
        for (key in obj) {
          if (obj.hasOwnProperty(key)) size++;
        }
        return size;
      };
      
      (function(){
        "use strict";
        window.checkAnswers = function(opts){
          
          function validateInput(){
            var question,
                answer;
                
            for (question in opts) {
              if(opts.hasOwnProperty(question)) {
                answer = f.elements[question].options[f.elements[question].selectedIndex].value;
                if(answer === "na"){
                  opts[question].state = "not-filled-in";
                } else if(answer === opts[question].answer){
                  opts[question].state = "correct";
                } else {
                  opts[question].state = "error";
                }
              }
            }
          }
          
          function markCorrectOrIncorrect(){
            var question, 
                li;
            
            for (question in opts) {
              if(opts.hasOwnProperty(question)) {
                var img = new Image(),
                li = f.elements[question].parentElement,
                feedbackImg = li.getElementsByTagName('img')[0];

                if (feedbackImg){
                  li.removeChild(feedbackImg);
                }
                
                if(opts[question].state === "correct"){
                  img.src = "http://www.littletherese.com/tick.jpg";
                  li.appendChild(img)
                } else if(opts[question].state === "error"){
                  img.src = "http://www.littletherese.com/x.jpg";
                  li.appendChild(img)
                }
              }
            }
          }
          
          function displayScore(){
            var correct = 0,
                error = 0,
                score = document.getElementById("score"),
                totalQuestions = Object.size(opts),
                question;

            for (question in opts) {
              if(opts.hasOwnProperty(question)) {
                if(opts[question].state === "correct"){
                  correct ++
                } else if(opts[question].state === "error"){
                  error ++
                }
              }
            }
            score.innerHTML = "You got " + correct + " out of " + totalQuestions;
          }
          
          function init(){
            validateInput();
            markCorrectOrIncorrect();
            displayScore();
          }
          
          init();
        }
      }());

      f.onsubmit = function(){
        checkAnswers({
          question1: {answer: "a"},
          question2: {answer: "b"},
       
        })
        return false;
      }
      
    f.reset.onclick = function(){
  var imgs = document.querySelectorAll('img');
  for(var i=0; i<imgs.length; i++){
    imgs[i].parentNode.removeChild(imgs[i]);
  }

  document.getElementById("score").innerHTML = "";
}
      
    </script>
</body>
</html>


I’ve just shown the image by default but you would of course append it with your js routine as per normal. The image will need to be smaller to fit as shown in the demo above.

Now that I see the ticks inside the dropdown box, I think it looks better outside, as in this previous code:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<style type="text/css">
 body { font-family:arial, helvetica, sans-serif; font-weight:bold; font-size:16px; color:#000; text-align:left; margin:3px 0px; }
 input { font-size:16px; width : 100px; padding-left : 4px;}
 #pagewrap  { width : auto; height:500px; margin-left:20px;  }
 #score { margin-top:10px; }
 ol li { padding:2px; 
      
       display: inline-block;
       } 
    ol li {
	padding:2px;
	display: inline-block;
	padding-right:30px;
	position:relative;
  background : #eeeeee;
}
li img{position:absolute;right:0;top:50%;margin-top:-8px}
 
</style>
</head>

<body>

<div id="pagewrap">
<p>Type in the text boxes below</p>
  <form>
     <ol>
    <li>  He  <input id="q1" type="text" size="auto" >  like football. </li>
    <li>  He <input id="q2" type="text" size="auto" >  got any money. </li>
     
     </ol>
  
    <input type="submit" id="submit" value="Result">
    <input type="reset" class="reset" id="reset" name="reset" value="Start again">
  </form>
  <div id="score">
    <!-- writes to here -->
  </div>
</div>
<!-- end pagewrap -->
<script type="text/javascript">
 A= new Array();   // preload images
 A[0]=new Image(28,15); A[0].src="http://www.littletherese.com/tick.jpg";
 A[1]=new Image(22,15); A[1].src="http://www.littletherese.com/x.jpg";
//
$("form").on("submit", function(e){ e.preventDefault(); $("img").remove(); markAnswers(); });
// -----
$("input[type=reset]").on("click", function(){ $("img").remove(); $("#score").html(""); });
// ------
var answers = {
"q1":["doesn't", "doesn´t", "does not"], 
"q2":["hasn't","has not"] 
} 

var numQuestions=0, questionsCorrect=0; 
//
  function markAnswers()
    { $("input[type='text']").each( function(){
           // add one to questions counter on each pass
             numQuestions++;
             if($.inArray(this.value, answers[this.id]) == -1)
                  { $(this).parent().append('<img src="'+A[1].src+'">'); } 
             else { $(this).parent().append('<img src="'+A[0].src+'">'); 
                   // add one to correct answer counter on each pass
                    questionsCorrect++; }
            // display scores        
             $("#score").html("You got " + questionsCorrect + " out of " + numQuestions);     
        } );
   // reset counters after marking is complete   
      numQuestions=0; questionsCorrect=0;      
   } 
//                    
</script>
</div>

</body>

</html>


I think this will be a good blueprint to use. Thanks!

oh, sorry I got confused between texboxes and dropdowns. This is the dropdown one:


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
  
       
 
   <title>English</title>
   
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  	<meta name="robots" content="index, follow"/>
   <meta name="description" content=""/>
<meta name="author" content="johnk" />

<meta name="keywords" content=""/>


    
    
    <style>
     ol li {
	padding:2px;
	display: inline-block;
	padding-right:30px;
	position:relative;
}
li img{position:absolute;right:0;top:50%;margin-top:-8px}
/*.min_pc_1 { width : 260px; }
.min_pc_2 { width : 290px; }
*/
    </style>

  </head>
  <body>
    <div id="pagewrap"> 
      <div id="container">
        <div id="content">
           
    
       
	  				

          <h4>For, since or ago?</h4>
          <div class="scroll">
            <form method="post" id="myForm" name="f">
              <ol>
                <li>
               It's been raining 
                  <select name="question1">
                    <option value="na">&nbsp;</option>
                    <option value="a"> for </option>   
                    <option value="b"> since </option>
                      <option value="c"> ago </option>  
                  </select> 
                 2 hours.
                </li>
                <li>
                  They have lived here
                  <select name="question2">
                   <option value="na">&nbsp;</option>
                    <option value="a"> for </option>   
                    <option value="b"> since </option>
                      <option value="c"> ago </option>  
                  </select>
                 1990.
                </li>
                
              </ol>
            
             <input type="submit" id="submit" value="Result" />
              <input type="reset" class="reset" id="reset" name="reset" value="Start again" />
            </form>
             <div id="score"></div> 
               <div class="address">
           
            </div>
          </div> 
        </div> 
      </div> 
    </div>
    
    <script>
      Object.size = function(obj) {
        var size = 0, key;
        for (key in obj) {
          if (obj.hasOwnProperty(key)) size++;
        }
        return size;
      };
      
      (function(){
        "use strict";
        window.checkAnswers = function(opts){
          
          function validateInput(){
            var question,
                answer;
                
            for (question in opts) {
              if(opts.hasOwnProperty(question)) {
                answer = f.elements[question].options[f.elements[question].selectedIndex].value;
                if(answer === "na"){
                  opts[question].state = "not-filled-in";
                } else if(answer === opts[question].answer){
                  opts[question].state = "correct";
                } else {
                  opts[question].state = "error";
                }
              }
            }
          }
          
          function markCorrectOrIncorrect(){
            var question, 
                li;
            
            for (question in opts) {
              if(opts.hasOwnProperty(question)) {
                var img = new Image(),
                li = f.elements[question].parentElement,
                feedbackImg = li.getElementsByTagName('img')[0];

                if (feedbackImg){
                  li.removeChild(feedbackImg);
                }
                
                if(opts[question].state === "correct"){
                  img.src = "http://www.littletherese.com/tick.jpg";
                  li.appendChild(img)
                } else if(opts[question].state === "error"){
                  img.src = "http://www.littletherese.com/x.jpg";
                  li.appendChild(img)
                }
              }
            }
          }
          
          function displayScore(){
            var correct = 0,
                error = 0,
                score = document.getElementById("score"),
                totalQuestions = Object.size(opts),
                question;

            for (question in opts) {
              if(opts.hasOwnProperty(question)) {
                if(opts[question].state === "correct"){
                  correct ++
                } else if(opts[question].state === "error"){
                  error ++
                }
              }
            }
            score.innerHTML = "You got " + correct + " out of " + totalQuestions;
          }
          
          function init(){
            validateInput();
            markCorrectOrIncorrect();
            displayScore();
          }
          
          init();
        }
      }());

      f.onsubmit = function(){
        checkAnswers({
          question1: {answer: "a"},
          question2: {answer: "b"},
       
        })
        return false;
      }
      
    f.reset.onclick = function(){
  var imgs = document.querySelectorAll('img');
  for(var i=0; i<imgs.length; i++){
    imgs[i].parentNode.removeChild(imgs[i]);
  }

  document.getElementById("score").innerHTML = "";
}
      
    </script> 
  </body>
</html>

I’m happy with this. Thanks!

Glad we could help :slight_smile: