开发者

How do i add text to span using jquery

开发者 https://www.devze.com 2023-04-08 01:26 出处:网络
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:

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 t开发者_StackOverflowo 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. i.e multiple="multiple"


Try something like:

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

JS Fiddle demo.

References:

  • change().
  • prev().
  • addClass().
  • text().
  • val().


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消