0

I am new to html dom that's why i don't know too much about it. I want to execute two functions in single html dom event. Here is the code.

<a id="link1" href="javascript:void(0)" onclick="document.getElementById('link1').style.background-color='red'; 
setTimeout(function () {window.location.assign='http://www.google.com'}, 5000);"> Read it</a>

Here i want to execute two functions. One is background color and second is opening google after 5 seconds, How can i get it in html dom. Please help me thanks.

5
  • why you don't use onclick="fnName()"? Commented Jan 14, 2014 at 14:49
  • when i use fnName() its get executed in multiple pages. I want to execute it on only 3 pages. Commented Jan 14, 2014 at 14:51
  • 2
    You should really not be hardcoding Javascript into onclick attributes, you should be writing pure Javascript in a .js file, attaching event handlers programmatically. Then the answer would be more obvious too. Commented Jan 14, 2014 at 14:51
  • assign loads a new page in same window Thanks Commented Jan 14, 2014 at 14:53
  • 1
    window.location.assign is a function. You need to call it. window.location.assign('http://www.google.com');. If you want to use =, you just do window.location='http://www.google.com';. developer.mozilla.org/en-US/docs/Web/API/Window.location Commented Jan 14, 2014 at 14:53

1 Answer 1

1

I fixed your code:

<a id="link1" href="javascript:void(0)" onclick="document.getElementById('link1').style['background-color']='red'; 
setTimeout(function () {window.location.assign('http://www.google.com'), 5000);"> Read it</a>
  1. background-color has a dash and it is not a valid identifier so you need to use the bracket notation.
  2. window.location.assign is a function.
Sign up to request clarification or add additional context in comments.

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.