0
var x = document.getElementById("selectCity"); 
var options = ["Bangalore", "Pune", "Kolkata"]; 

for(var i = 0; i < options.length; i++) {
    var opt = options[i];
    var element = document.createElement("option");
    element.textContent = opt;
    element.value = opt;
    x.appendChild(element);
}

$('select[name="cityDropdown"]').change(function(){
  cityName=$(this).val();
 });

Now with each city i want to store my circle name. and save it as an attribute and pass when the city is selected

1
  • i want to hardcode to a list of circle & city like 1.Lucknow UP 2. patna : bihar jharkhand 3. chdigard : punjab city name should be displayed in dropdown and when i select and proceed my cityname as well as circle name should be passed Commented Apr 21, 2015 at 6:43

4 Answers 4

1

You can Hardcode a custom attribute to a select's option. For example,

<option circle="UP" value="Lucknow">Lucknow</option>

and get it's value with jquery like this,

var circle = $('option:selected', this).attr("circle");

HTML

<select name="cityDropdown">
  <option circle="UP" value="Lucknow">Lucknow</option>
  <option circle="Bihar" value="Patana">Patana</option>
  <option circle="Punjab" value="Chandigarh">Chandigarh</option>
</select> 

Javascript

$('select[name="cityDropdown"]').change(function(){
    var cityName = $(this).val();
    var circle = $('option:selected', this).attr("circle");
    console.log(cityName + " : " + circle);
});

Note : You can also use other custom attribute (custom attribute city for example). But you just need to use city as value of the option.

Here is the fiddle.

Sign up to request clarification or add additional context in comments.

Comments

0

make a proper selector ..try this ..

 $('#selectCity').change(function(){
       cityName = $(this).val(); 
       console.log(cityName); // do something
     });

HERE is the fiddle..

1 Comment

no my question is how do i make my city list a 2d array so that i save city as well as circle. and in my dripdown it gets called as <option circle="CircleName" city="cityName">cityName</option>
0
var x = document.getElementById("selectCity"); 
var options = ["Bangalore", "Pune", "Kolkata"]; 

for(var i = 0; i < options.length; i++) {
    var opt = options[i];
    var element = document.createElement("option");
    element.textContent = opt;
    element.value = opt;
    element.setAttribute('circle-name', 'your value'); // your attribute
    x.appendChild(element);
}

$('select[name="cityDropdown"]').change(function(){
  cityName=$(this).val();
 });

Comments

0
var optionAttr = $('#cityDropdown option:selected').attr("circle");
var optionAttr1 = $(this).find('option:selected').attr("circle");

2 Comments

Why suddenly use $(this)?
You answer could be improved by including a description of your code so that others can understand what it does and how it addresses the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.