Jump to content
Killersites Community
Sign in to follow this  
Jorge

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;
    element.focus();    
}

Or this one:

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

 

This is the HTML just in case:

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

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

</head>
<body>

    <main>

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

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

    </main>

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

</body>
</html>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×