0

I'm looking for load a function while i click on a button. I'm learning JS ...

function changeStyle(){
 var paragraphe = document.getElementById('monParagraphe');
 paragraphe.style.backgroundColor="#000";
}
 
function load(){
 var buttonElt = document.getElementsByTagName('button');
 buttonElt.addEventListener('click', changeStyle)
}
<p id=’monParagraphe’>Lorem ipsum dolor sit amet, consectetur adipisicing
 elit, sed do eiusmod
 tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
 quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
 consequat.
</p>
<button onclick=changeStyle()>Changer le style</button>
 

Jeremy Thille
26.4k12 gold badges48 silver badges64 bronze badges
asked Oct 9, 2017 at 11:59
7
  • 2
    Use id="monParagraphe" instead of id=‘monParagraphe’. Commented Oct 9, 2017 at 12:01
  • You are binding the click listener twice. Either do the load() fuction or the onclick=... Commented Oct 9, 2017 at 12:03
  • my god ... i'm so stupid ! Thank you very much :) Commented Oct 9, 2017 at 12:03
  • Why are you getting those weird instead of regular quotes? Are you writing your JS in Word? Commented Oct 9, 2017 at 12:04
  • Possible duplicate of alert pop-up not showing Commented Oct 9, 2017 at 12:05

1 Answer 1

1

Wrong tokens are used "".

function changeStyle(){
 var paragraphe = document.getElementById('monParagraphe');
 paragraphe.style.backgroundColor="#000";
}
function load(){
 var buttonElt = document.getElementsByTagName('button');
 buttonElt.addEventListener('click', changeStyle)
}
<p id="monParagraphe">Lorem ipsum dolor sit amet, consectetur adipisicing
 elit, sed do eiusmod
 tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
 quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
 consequat.
</p>
<button onclick="changeStyle()">Changer le style</button>

answered Oct 9, 2017 at 12:03
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.