简体   繁体   中英

How do i add text to span using jquery

I am trying to add the selected value of combo box to its preceding span. but it failed too sad. I am trying with following code:

<span></span><select multiple ="multiple" ondblclick="$(this).css('display','none').prev().css('display','inline').addClass('sss').html($(this).val());">....</select>

What's the error here? how can i do it easily?

I want it to trigger in double click event. as after double clicking on option of combo box it should disappear and selected text should appear in span

and of course i forgot to tell that my select box is not selectbox. its a lisstbox. ie multiple="multiple"

Try something like:

$('select').change(
    function() {
        $(this).prev('span').addClass('sss').text($(this).val());
    });

JS Fiddle demo .

References:

You need to put an option in your select tag.

<span></span>

<select>
    <option>Something</option>
</select>

js

 
 
 
 
  
  
  $('select').click(function(){ $(this).css('display','none') .prev() .css('display','inline') .addClass('sss') .html($(this).val()); });
 
 
  

Example: http://jsfiddle.net/jasongennaro/AKa9b/

EDIT

Based on the comment not working in context of multiple option , I revised as follows:

$('select').change(function(){
    $('select option:selected').each(function(){
       $(this).parent().css('display','none')
      .prev()
      .css('display','inline')
      .addClass('sss')
      .html($(this).val());
    });
}); 

Second example: http://jsfiddle.net/jasongennaro/AKa9b/2/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM