Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing select option displayed text after it is selected but return when changing the selection

I want to show list of countries with select options like this:

<select id="country_code">
    <option value="358"> Finland(+358)</option>
    <option value="33"> France(+33)</option>
    <option value="43"> Austria(+43)</option>
</select>

When user selects a country, I want only the code to be displayed as the selected option.

This is what I've tried:

 $("#country_code").change(function(){
        $(this).find("option:selected").text("+"+$(this).find("option:selected").text().match(/(\d+)/g));

    })

This code is separating the Code (regex is for separating the number) from selected option and displays it instead of the full name, and it works.

But the problem is when I try to select some other countries, the previous options have changed! and i can not get it back.

it look like this :

<select id="country_code">
    <option value="358"> +358</option>
    <option value="33"> France(+33)</option>
    <option value="43"> +43</option>
</select>

How should I accomplish this? I have searched online with no results.

like image 301
Ahmad Mobaraki Avatar asked Dec 28 '16 06:12

Ahmad Mobaraki


People also ask

How do I change the Select option?

In order to change the selected option by the value attribute, all we have to do is change the value property of the <select> element. The select box will then update itself to reflect the state of this property. This is the easiest and most straightforward way of updating a select box state.

How do I get selected text option in select?

Use the selectedIndex Property We can get the index of the selected item with the selectedIndex property. Then we can use the text property to get the text of the selected item. We create the getSelectedText function that takes an element as the parameter. Then we check if selectedIndex is -1.

How do you modify a selection in HTML?

Set the value Property of the Select Drop Down We can change the HTML select element's selected option with JavaScript by setting the value property of the select element object. We have a select drop down with some options and 2 buttons that sets the selected options when clicked.


2 Answers

My version: :)

    $( "#country_code" ).change( function()
    {
        var $this = $( this )

        $this.find( "option:first" ).text( '+' + $this.val() ).val( $this.val() ).prop( 'selected', true )
    } )
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="country_code">
    <option value="" style="display: none;" selected="selected">select a country</option>
    <option value="358"> Finland(+358)</option>
    <option value="33"> France(+33)</option>
    <option value="43"> Austria(+43)</option>
</select>
like image 120
tiomno Avatar answered Sep 19 '22 12:09

tiomno


You can store the previous selected data.
like this:

 var $previousOption,storedText;
$("#country_code").change(function(){
    if($previousOption!=null){
       $previousOption.text(storedText)
    }   
      $previousOption=$(this).find("option:selected");
      storedText=$previousOption.text(); 

$(this).find("option:selected").text("+"+$(this).find("option:selected").text().match(/(\d+)/g));
    })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="country_code">
    <option value="358"> Finland(+358)</option>
    <option value="33"> France(+33)</option>
    <option value="43"> Austria(+43)</option>
</select>
like image 29
Ying Yi Avatar answered Sep 18 '22 12:09

Ying Yi