0
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");      <-- ?
}
1
  • 3
    name of div? Any reason why you want to do this? Commented Oct 13, 2010 at 10:01

2 Answers 2

4

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.

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

Comments

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

Comments

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.