0

I have 2 buttons on a page that I can't change code:

<button type="submit" name="next" class="button">Next</button>
<button type="submit" name="back" class="button">Previous</button>

On the back button I want to use javascript to reset the values of fields on that page by using:

document.getElementById(setV).value = "0"

I need to know how to do this on the name of a button.

I can't use getElementById or class as the name is the only thing to distinguish them. I have tried the follwing to take action but no joy:

$('[name="next"]').onclick();
8
  • 2
    I confused about what the question is? Commented Apr 17, 2018 at 10:10
  • is this what you want $('[name="next"]').click(function(){$(this).val()}) Commented Apr 17, 2018 at 10:12
  • You can use document.querySelectorAll('button')[1].click(). Or you nearly got it $('button[name="next"]').click();. Commented Apr 17, 2018 at 10:12
  • @ThisGuyHasTwoThumbs He said he cannot change the HTML. Commented Apr 17, 2018 at 10:13
  • @AjAX. Why not querySelector('[name="next"]')? Commented Apr 17, 2018 at 10:17

4 Answers 4

1

Why don't you use

<input type="reset" value="Reset">

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

Comments

0

You can try this.

$(document).ready(function(){
 $(document).on("click","button[name='next']",function(){
   $("#setV").val(0);
 });
});

Comments

0

You can try:

var backButton = document.querySelector("[name='back']")

to get the element with the name attribute. Then to change the value add a listener to the backButton element by:

backButton.addEventListener("click", function(){
  document.getElementById('setV').value = '0';
});

Comments

0

1. Using javascript:

var backButton = document.querySelector("[name='back']")
backButton.addEventListener('click',function(){
 document.getElementById(setV).value = "0"
 // your code
}, false);

2. Using jQuery:

$(":button[name='back']").click(function(){
  document.getElementById(setV).value = "0"
  // your code
})

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.