0

Using Codepen.

When the form submit button is pressed, JavaScript instructs my 'result' div to become visible (by removing the 'hidden' attribute):

const form = document.getElementById("form")

form.addEventListener('submit', function(event) {
  const result = document.getElementById("result");

  result.removeAttribute("hidden");

  event.preventDefault();
})
<form id="form">
  <input type="submit" value="Submit">
</form>

<div hidden id="result">Result</div>

Works absolutely fine on Desktop. Press the button and the word 'Result' appears. Doesn't work on mobile - the word doesn't appear.

UPDATE: I should add - this issue is only reproduced on Codepen. When I run this code here on stack overflow on mobile it works fine. So the issue must be Codepen related? I haven't changed any of the Codepen defaults to get this issue

3
  • Which mobile browser? Commented Apr 26, 2024 at 13:28
  • Android, Chrome Commented Apr 26, 2024 at 13:38
  • Please post the codepen - the code works here Commented Apr 29, 2024 at 5:20

1 Answer 1

0

Your script works fine on iPhone - perhaps an Android issue?

Let's however move the preventDefault and change the removeAttribute to setting hidden false which is simpler code

const form = document.getElementById("form")
const result = document.getElementById("result");
form.addEventListener('submit', function(event) {
  event.preventDefault();
  result.hidden = false;
})
<form id="form">
  <input type="submit" value="Submit">
</form>

<div hidden id="result">Result</div>

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

3 Comments

Did you try in Codepen? This snippet also worked for me in Android here on Stack Overflow. Is it something about the iframe that Codepen adds or something...?
And thank you for the tips on simplifying my code
I think we lost or never had your codepen link?

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.