0

How to change the button's name based on the selected values? This function I found returns null.

function myFunction(new_id) {
  if (new_id == 0) {
    return;
  }
  document.getElementById("submit").id = new_id;

  // test new id
  // remove this after testing
  alert(document.getElementById(new_id).id);
}
<select id="select" name="select" class="btn btn-block gray dropdown-toggle" required onchange="myFunction(this.value)">
  <option Disabled value="0" selected="selected">- Select Me -</option>
  <option value="5"> 5</option>
  <option value="10"> 10</option>
</select>
<button type="submit" id="submit" name="">Button</button><br>

2
  • Looks like it works at least once. Which makes sense. Commented Sep 4, 2020 at 21:16
  • Wouldn't it make more sense to change the button's value rather than its name? Commented Sep 4, 2020 at 21:19

1 Answer 1

1

You shouldn't be changing the button's ID, you should change its name.

function myFunction(new_id) {
  if (new_id == 0) {
    return;
  }
  document.getElementById("submit").name = new_id;
  alert(document.getElementById("submit").name);
}
<select id="select" name="select" class="btn btn-block gray dropdown-toggle" required onchange="myFunction(this.value)">
  <option Disabled value="0" selected="selected">- Select Me -</option>
  <option value="5"> 5</option>
  <option value="10"> 10</option>
</select>
<button type="submit" id="submit" name="">Button</button><br>

However, changing the submit button's name seems like a poor design. I think it would be better to change its value.

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

1 Comment

this works for me .. and yeah value is not working with post to another php name define only works

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.