Jump to content

Which way is more elegant


Recommended Posts

Can I please get your opinion on this piece of js code.

Which way is better or correct between the two version, I know both work but I am curious if of one of the 2 is better or both or if none.

This one:

document.getElementById("startEd").onclick = function startEdit () {
    //Make the element editable
    var element = document.getElementById("editableElement");
    element.contentEditable = true;

Or this one:

 function startEdit () {
    //Make the element editable
    var element = document.getElementById("editableElement");
    element.contentEditable = true;
//Runs the StartEdit function when button is clicked
document.getElementById("startEd").onclick = function() {


This is the HTML just in case:

<html lang="en">
    <meta charset="UTF-8">
    <title>Editable Content</title>

    <link rel="stylesheet" href="css/editableElement.css">



        <button id="startEd">Start Editing</button>
        <button id="stopEd">Finish Editing</button>

    <p id="editableElement"></p>


<script type="text/javascript" src="js/editableElement.js"></script>    


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...