-1

I having a dropdown like this

<select name="phoneCallingCode" id="phoneCallingCode" class="input">
   <option value="93">Afghanistan (+ 93)</option>
   <option value="355">Albania (+ 355)</option>
   <option value="213">Algeria (+ 213)</option>
</select>

when I select a value Afghanistan (+ 93), I just want to show +93 in the display, it that possible through jquery.

Thanks in advance.

enter image description here

5 Answers5

0

Okay from my understanding you want to show number when anyone selects country in drop down.

So you can do something like this:

$('#phoneCallingCode').change(function() {

    $("option:selected", this).text($("option:selected", this).val());

});

But remember this will change selected option text too, so user may have hard time finding country name again.

Fiddle: http://jsfiddle.net/NyHts/

Abhishek
  • 5,649
  • 3
  • 23
  • 42
0

Working demo

Code

//For starting of page
  valNum=$("#phoneCallingCode").val();
   $("#nmbr").val(valNum);

$("#phoneCallingCode").change(function(){
   valNum=$(this).val();
   $("#nmbr").val(valNum);
});

It means,Get value of selected Drop down option and Set the value of Textbox by the value in valNum variable.

And when document is ready , u need contry Number in textbox so The code //For starting of page

Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
0

Use as below

$('#phoneCallingCode').change(function() {
    $("#ShowNumber").val("+" +$("#phoneCallingCode option:selected").val())
});

HTML

<select name="phoneCallingCode" id="phoneCallingCode" class="input">
   <option value="93">Afghanistan (+ 93)</option>
   <option value="355">Albania (+ 355)</option>
   <option value="213">Algeria (+ 213)</option>
</select>
<input type="text" id="ShowNumber"/>

DEMO

Amit
  • 15,217
  • 8
  • 46
  • 68
0
$('#phoneCallingCode').change(function(){
        var selectedOptionVal = $(this).val();
        var temp = "[value='"+selectedOptionVal+"']";
        var selected = $(temp).html();
        if (selected.indexOf("(+") != -1) {
            var selectedString     = selected.substring(selected.indexOf("(+")+1,selected.length-1);
            $(temp).html(selectedString);
        }
    });
Sanjay Kumar N S
  • 4,653
  • 4
  • 23
  • 38
0

You can accomplish this task using HTML5 data attribute as below:

<select name="phoneCallingCode" id="phoneCallingCode" class="input" onclick="resetPhoneCallingCode()" onblur="displayContryCode();">
    <option value="93" data-text="Afghanistan (+ 93)">Afghanistan (+ 93)</option>
    <option value="355" data-text="Albania (+ 355)">Albania (+ 355)</option>
    <option value="213" data-text="Algeria (+ 213)">Algeria (+ 213)</option>
</select>

<script>
    function resetPhoneCallingCode() {
        $("#phoneCallingCode option").each(function () {
            $(this).text($(this).attr('data-text'));
        });
    }
    function displayContryCode() {
        $("#phoneCallingCode option:selected").text("+" + $("#phoneCallingCode").val());
    }
</script>
Hiren Kagrana
  • 912
  • 8
  • 21