oblongsquare
oblongsquare

Reputation:

How can I simplify this Javascript function?

I'm trying to scale this but I'm a little green when it comes to creating JS functions. I managed to get this far on my own, but alas...

When a span gets a value of whatever (in this case, a city name), I want the select box to automatically match it. And I want to scale this by getting rid of all these else if's.

$(document).ready(function() {
  $('select#field1 option').removeAttr('selected');
  var whereEvent = $('span#field2').html();
  if (whereEvent == 'New York') {
    $('select#field1 option[value=New York]').attr('selected', 'true');
  } else if (whereEvent == 'Los Angeles') {
    $('select#field1 option[value=Los Angeles]').attr('selected', 'true');
  } else if (whereEvent == 'Tokyo') {
    $('select#field1 option[value=Tokyo]').attr('selected', 'true');
  } else if (whereEvent == 'London') {
    $('select#field1 option[value=London]').attr('selected', 'true');
  } else if (whereEvent == 'Sydney') {
    $('select#field1 option[value=Sydney]').attr('selected', 'true');
  } else if (whereEvent == 'Paris') {
    $('select#field1 option[value=Paris]').attr('selected', 'true');
  }
});

Can someone help me out here? I promise I'll be grateful for your help. Thank you.

Upvotes: 1

Views: 216

Answers (4)

TrL
TrL

Reputation: 30

This will work at least, and it will seem logical as well.

$(document)
.ready(function () {
$("select#field1 option")
    .removeAttr("selected");
var a = $("span#field2")
    .html();
a == "New York" ? $("select#field1 option[value=New York]")
    .attr("selected", "true") : a == "Los Angeles" ? $("select#field1 option[value=Los Angeles]")
    .attr("selected", "true") : a == "Tokyo" ? $("select#field1 option[value=Tokyo]")
    .attr("selected", "true") : a == "London" ? $("select#field1 option[value=London]")
    .attr("selected", "true") : a == "Sydney" ? $("select#field1 option[value=Sydney]")
    .attr("selected", "true") : a == "Paris" && $("select#field1 option[value=Paris]")
    .attr("selected", "true")
})

Upvotes: 0

Jaime Febres
Jaime Febres

Reputation: 1267

This is an option:

    $(document).ready(function() {
      $('select#field1 option').removeAttr('selected');
      var whereEvent = $('span#field2').html();
      var filter = 'select#field1 option[value=' + whereEvent + ']';
      $(filter).attr('selected', 'true');
   }

Or you could do:

    $(document).ready(function() {
      var filter = $('span#field2').html();
      $('select#field1 option').each(function(){
           this.selected = (this.value == filter);
      });
   }

Upvotes: 5

Andy Hume
Andy Hume

Reputation: 41664

I don't know jQuery, so there's probably a more elegant way, but:

var whereEvent = $('span#field2').html();
$('select#field1 option[value=' + whereEvent + ']').attr('selected', 'true');

Upvotes: 0

Ionuț Staicu
Ionuț Staicu

Reputation: 22164

Maybe:

$('select#field1 option').removeAttr('selected');
var whereEvent = $('span#field2').html();
$('select#field1 option[value='+whereEvent+']').attr('selected', 'true');

?

Upvotes: 3

Related Questions