1

I was trying to run two JS functions in one click using this. Can anybody show me how to do it correctly?

<button onclick="getlocation" onclick="showDiv()">Try It</button>

I tried adding a ; between the two functions but it didnt work too.

  • add both methods in onclick – Ramanlfc Dec 07 '15 at 16:34
  • Can you just write me the code? –  Dec 07 '15 at 16:34
  • Ha ha ha ha ha - erm, for a fee anyone will do anything – StudioTime Dec 07 '15 at 16:34
  • `onclick="getLocation() showDiv() " ` – Ramanlfc Dec 07 '15 at 16:35
  • Can you show us the code of the functions `getLocation()` and `showDiv()` ? – CoderPi Dec 07 '15 at 16:36
  • @Quentin that's not a doublicate!!! Read the question! – CoderPi Dec 07 '15 at 16:37
  • Check out: http://jsfiddle.net/b007ksLf/2/ – Lado Lomidze Dec 07 '15 at 16:38
  • @CodeiSir — I have read the question. Twice. Unless there is a problem with the functions themselves (which isn't expressed in the question and so is irrelevant unless and until the question is edited) which would cause the solution to the duplicate to not work here, it is exactly the same question. – Quentin Dec 07 '15 at 16:39
  • @Quentin did you read this "Adding ";" semicolon didn't work, if you could help.." ... ? – CoderPi Dec 07 '15 at 16:40
  • @CodeiSir — Of course it didn't. The problem is that the code in the question has multiple attributes with the same name, not that the trailing semicolon was missing from the JavaScript statements in the onclick attributes. – Quentin Dec 07 '15 at 16:42
  • @Quentin now I see your point. You are interpreting the sentence diffrently.. for me it was that he already tryed the semicolon like that `onclick="getLocation();showDiv()"` – CoderPi Dec 07 '15 at 16:46

1 Answers1

0

"Adding ";" semicolon didn't work, if you could help.."

It works for sure, you have to do it like that: onclick="getLocation();showDiv()"


Was not relevant in this case:

The problem could be, that both functions are setting the content of your div, so the second function will overwrite the content!

Use .innerHTML += "..."; on you second function, this will append the content to the exisitng content.

CoderPi
  • 12,985
  • 4
  • 34
  • 62
  • Check it yourslef.. I have added ";". Http://whatscast.in/apps/locateme –  Dec 07 '15 at 16:38
  • Reload 2 times and see. –  Dec 07 '15 at 16:38
  • No.. It runs Both Js sometimes and Only one sometimes.. –  Dec 07 '15 at 16:40
  • "The problem is, that both function are setting the content of your div" — That is not true. It isn't implied by question, and the live demo the OP provided shows that only one function modifies innerHTML, the other just changes the style/display property. – Quentin Dec 07 '15 at 16:41
  • @Quentin I saw that now – CoderPi Dec 07 '15 at 16:42
  • For me the code works fine, sorry @NitinJain - the problem is not what you asked in the question. `onclick="getLocation();showDiv()"` works fine, i can guarantee you that. If you have issues ask a new question – CoderPi Dec 07 '15 at 16:44
  • http://imgur.com/dEMpzb9 http://imgur.com/kcuXKg8 http://imgur.com/rqUvSAV http://imgur.com/Tq9YrXP –  Dec 07 '15 at 16:44
  • Sorry but the button "Refresh Location" is shown wich is you div of you second function – CoderPi Dec 07 '15 at 16:47
  • Hey, I'm sorry. Now my code is working fine.. Thank you. But, the Refresh button shows up before the geolocation, any way to display it after the geolocation? –  Dec 07 '15 at 16:48
  • If this is working fine, don't forget to mark the answer as accepted. You should now really post a new question for "any way to display it after the geolocation?" and don't forget to post the code, so people can help you! – CoderPi Dec 07 '15 at 16:50