85

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? Commented Feb 24, 2014 at 23:50
  • 5
    Why? Inline code is horrible and non-maintainable or reusable Commented Feb 24, 2014 at 23:55
  • 4
    because I am echoing it in the page and I want to keep it constrained. and I dont want to repeat doSomething multiple times. Commented 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. Commented Feb 25, 2014 at 0:01
  • 3
    This makes sense for a quick test script (sometimes). Commented May 18, 2018 at 18:13

5 Answers 5

137

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>

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

5 Comments

Upvoting this but... really, I hope you have a good reason you're writing inline code, it's usually bad practice.
^^ 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.
My use case for this is for dismissing a loading splash screen. The onclick deletes the splash screen from the DOM.
@JaredMenard try to replace anchor tag with div.
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
36

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

Comments

17

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>

Comments

13

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.

4 Comments

I tried this and in firefox I get: SyntaxError: function statement requires a name
would this test function be heard outside of href scope?
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.
I found the solution helpful, indeed. Having a bunch of websites. Simply copying the whole html line without worrying about browser caching as the links already have gone out to the client…Nice and dirty.
1

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>

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.