views:

57

answers:

2
if (selectedCountry == "USA") {
  $("#state").show();
}

How can I change name for my div #state with the same condition? (to add name="NewName" to my div id="state")

Smth like:

if (selectedCountry == "USA") {
  $("#state").show();
  $("#state").name("NewName");      <-- ?
}
A: 
if (selectedCountry == "USA") {
  $("#state").show();
  $("#state").attr("name","NewName");
}
Arnis L.
Thats works! Thanks.
FlashTrava
+3  A: 

Use .attr() to set attributes on an element, for this:

if (selectedCountry == "USA") {
  $("#state").show().attr("name", "NewName");
}

If setting many at once, you can also pass an object, for example:

if (selectedCountry == "USA") {
  $("#state").show().attr({ name: "NewName" });
}

I'm assuming you meant a <select> here with a dropdown, since a <div> won't be serialized into a form a name attribute isn't useful or valid, if this isn't the select, just find the form element and perform the same .attr() call.

Nick Craver