76

Is there a way to write following code inline like so?

<a href="#" onClick="function(){
    //do something;
    return false;
};return false;"></a>

Instead of doing this:

 <a href="#" onClick="doSomething(); return false;"></a>

 function doSomething(){
    //do something;
 }
8
  • 11
    Why would you want to? Feb 24, 2014 at 23:50
  • 5
    Why? Inline code is horrible and non-maintainable or reusable
    – Josh Bedo
    Feb 24, 2014 at 23:55
  • 3
    because I am echoing it in the page and I want to keep it constrained. and I dont want to repeat doSomething multiple times.
    – Toniq
    Feb 24, 2014 at 23:57
  • 3
    @Toniq Why would you have to repeat doSomething multiple times? Declare it once. If you need to associate certain data with the <a>, use a data-* attribute and render information there. And don't set the onclick in the tag - wait for the DOM to be ready, get all <a> elements, and bind a click handler to each, calling doSomething and passing it the data-* attribute you may need.
    – Ian
    Feb 25, 2014 at 0:01
  • 2
    This makes sense for a quick test script (sometimes).
    – HartleySan
    May 18, 2018 at 18:13

5 Answers 5

125

You can use Self-Executing Anonymous Functions. This code will work:

<a href="#" onClick="(function(){
    alert('Hey i am calling');
    return false;
})();return false;">click here</a>

5
  • 4
    Upvoting this but... really, I hope you have a good reason you're writing inline code, it's usually bad practice.
    – quoo
    Nov 5, 2015 at 16:41
  • 2
    ^^ I can see it being bad practice when repeating code, but anonymous functions do have their place. I wrote a function that calls a custom boostrap confirm modal. I want it to handle a situation on cancel and confirm uniquely for that instance when I need it. It's silly to write the two functions elsewhere and have to search for them within the code when they're not reused.
    – eaglei22
    Jan 9, 2017 at 20:41
  • My use case for this is for dismissing a loading splash screen. The onclick deletes the splash screen from the DOM. Feb 28, 2018 at 15:21
  • @JaredMenard try to replace anchor tag with div. Mar 3, 2018 at 5:40
  • 2
    I just used this method to insert a shim for debugging into a page I don't control, so I couldn't add an extra function. It's ugly, but it works Nov 15, 2018 at 10:54
34

This should work:

 <a href="#" onclick="function hi(){alert('Hi!')};hi()">click</a>

You may inline any JavaScript inside the onclick as if you were assigning the method through JavaScript. I think is just a matter of making code cleaner keeping your js inside a script block

12

Based on the answer that @Mukund Kumar gave here's a version that passes the event argument to the anonymous function:

<a href="#" onClick="(function(e){
    console.log(e);
    alert('Hey i am calling');
    return false;
})(arguments[0]);return false;">click here</a>
11

This isn't really recommended, but you can do it all inline like so:

<a href="#" onClick="function test(){ /* Do something */  } test(); return false;"></a>

But I can't think of any situations off hand where this would be better than writing the function somewhere else and invoking it onClick.

3
  • I tried this and in firefox I get: SyntaxError: function statement requires a name
    – Toniq
    Feb 24, 2014 at 23:59
  • would this test function be heard outside of href scope?
    – Toniq
    Feb 25, 2014 at 0:03
  • 1
    I thought it'd exist in the global scope, but that doesn't seem to be true. As far as I can tell it won't exist outside the scope of that tag, but I'm not sure what scope it exists in for sure.
    – jvdub
    Feb 25, 2014 at 0:19
0

I know this thread is old, but I was looking for a solution to this today. When you're "inlining" JavaScript like below, don't include "function xx()" and such. See the below example. I tested it today, and it works perfectly without console errors or warnings.

Wanted to share a solution:

<a href="#" type="button" onClick="var myDiv = document.getElementById('myDIV'); if (myDiv.style.display === 'block') { myDiv.style.display = 'none'; } else { myDiv.style.display = 'block'; }">click</a>

In a standard HTML markup, the following would be a typical implementation of the "inlined JS" example above.

<body>
<a href="#" type="button" onclick="myFunction()">Try it</a>
...(this is where the rest of the html page content goes)....
<script>
function myFunction() {
  var x = document.getElementById("myDIV");
  if (x.style.display === "none") {
     x.style.display = "block";
  } else {
     x.style.display = "none";
  }
}
</script>
</body>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.