Home > Software design >  Removing readonly attribute inside 3 divs
Removing readonly attribute inside 3 divs

Time:05-16

i am creating a to-do list, i want the user to be able to edit and delete the added tasks, this will be possible by removing readonlyattribute from HTML. but i am struggling doing the edit function.

Here is my code:

    const formulario=document.getElementById("new-task-form");
    const lista=document.getElementById("list");
    const input=document.querySelector("#new-task-input");
    
    let id=0;
    
    formulario.addEventListener('submit', (e)=>{
        //console.log("activado");
        e.preventDefault();
        addTask();
    
    });
    
    let addTask=()=>{
        id  ;
        let task=input.value;
        lista.innerHTML =`<div id="tasks">
        <div  id="taskin">
             <div  id="${id}">
                 <input 
                 id="${id}"
                 type="text"
                 
                 value=${task}
                 readonly
                 />
             </div>
             <div >
                 <button >Edit</button>
                 <button >Delete</button>
             </div>
            </div> 
        </div> `
    
        input.value='';
    }
    
    
    
    lista.addEventListener('click', (evento)=>{
        console.log(evento);
        if(evento.target.className=="edit"){
            console.log("Editar");
        }else if(evento.target.className=="delete"){
            console.log("Borrar");
            
    
        }
    })
    <body>
    <header>
        <h1>Task List</h1>
        <form action="#" id="new-task-form"> <!--Formulario para poner tareas-->
            <input type="text" id="new-task-input" placeholder="Que vas a hacer" />
            <input  type="submit" id="new-task-submit" value="Add Task" />
        </form>
    </header>
        
    <main>
        <div id="list" >
            
            <h2>Tasks</h2>

            <div id="tasks">
               
               <!-- <div >
                    <div >
                        <input type="text" 
                        
                        value="My Task"
                        readonly
                        />
                    </div>
                    <div >
                        <button >Edit</button>
                        <button >Delete</button>
                    </div>
                </div> -->

            </div>

        </div>
    </main>
    <script src="app.js"></script>.
</body>
</html>

Thanks in advance :D

CodePudding user response:

I have added some lines to your JS code. Please check.

    lista.addEventListener('click', (evento)=>{
    let parentEl = evento.target.parentNode.parentNode;
    let input = parentEl.getElementsByTagName('input');
    if(evento.target.className=="edit"){
        console.log("Editar");
        input[0].removeAttribute('readonly');
    }else if(evento.target.className=="delete"){
        console.log("Borrar");
    }
    })

Test it in the Snippet below:

const formulario=document.getElementById("new-task-form");
    const lista=document.getElementById("list");
    const input=document.querySelector("#new-task-input");
    
    let id=0;
    
    formulario.addEventListener('submit', (e)=>{
        //console.log("activado");
        e.preventDefault();
        addTask();
    
    });
    
    let addTask=()=>{
        id  ;
        let task=input.value;
        lista.innerHTML =`<div id="tasks">
        <div  id="taskin">
             <div  id="${id}">
                 <input 
                 id="${id}"
                 type="text"
                 
                 value=${task}
                 readonly
                 />
             </div>
             <div >
                 <button >Edit</button>
                 <button >Delete</button>
             </div>
            </div> 
        </div> `
    
        input.value='';
    }
    
    
    
    lista.addEventListener('click', (evento)=>{
        let parentEl = evento.target.parentNode.parentNode;
        let input = parentEl.getElementsByTagName('input');
        console.log(input[0]);
        if(evento.target.className=="edit"){
            input[0].removeAttribute('readonly');
            console.log("Editar");
        }else if(evento.target.className=="delete"){
            console.log("Borrar");
            
    
        }
    })
<body>
    <header>
        <h1>Task List</h1>
        <form action="#" id="new-task-form"> <!--Formulario para poner tareas-->
            <input type="text" id="new-task-input" placeholder="Que vas a hacer" />
            <input  type="submit" id="new-task-submit" value="Add Task" />
        </form>
    </header>
        
    <main>
        <div id="list" >
            
            <h2>Tasks</h2>

            <div id="tasks">
               
               <!-- <div >
                    <div >
                        <input type="text" 
                        
                        value="My Task"
                        readonly
                        />
                    </div>
                    <div >
                        <button >Edit</button>
                        <button >Delete</button>
                    </div>
                </div> -->

            </div>

        </div>
    </main>
    <script src="app.js"></script>.
</body>
</html>

Hope it helps.

CodePudding user response:

  1. First, 2 elements have the same id 's value :

    <div id="${id}"> and <input id="${id}">

  2. You can change same as :

const formulario=document.getElementById("new-task-form");
    const lista=document.getElementById("list");
    const input=document.querySelector("#new-task-input");
    
    let id=0;
    
    formulario.addEventListener('submit', (e)=>{
        //console.log("activado");
        e.preventDefault();
        addTask();
    
    });
    
    let addTask=()=>{
        id  ;
        let task=input.value;
        lista.innerHTML =`<div id="tasks">
        <div  id="taskin">
             <div  id="${id}">
                 <input 
                 id="input-${id}"
                 type="text"
                 
                 value=${task}
                 readonly
                 />
             </div>
             <div >
                 <button  data-task_id="input-${id}">Edit</button>
                 <button  data-task_id="input-${id}">Delete</button>
             </div>
            </div> 
        </div> `
    
        input.value='';
    }
    
    
    
    lista.addEventListener('click', (evento)=>{
        if(evento.target.className=="edit"){
            document.getElementById(evento.target.dataset.task_id).removeAttribute('readonly');
        }else if(evento.target.className=="delete"){
            document.getElementById(evento.target.dataset.task_id).remove();
            
    
        }
    })
<body>
    <header>
        <h1>Task List</h1>
        <form action="#" id="new-task-form"> <!--Formulario para poner tareas-->
            <input type="text" id="new-task-input" placeholder="Que vas a hacer" />
            <input  type="submit" id="new-task-submit" value="Add Task" />
        </form>
    </header>
        
    <main>
        <div id="list" >
            
            <h2>Tasks</h2>

            <div id="tasks">
               
               <!-- <div >
                    <div >
                        <input type="text" 
                        
                        value="My Task"
                        readonly
                        />
                    </div>
                    <div >
                        <button >Edit</button>
                        <button >Delete</button>
                    </div>
                </div> -->

            </div>

        </div>
    </main>
    <script src="app.js"></script>.
</body>
</html>

CodePudding user response:

Maybe you mean remove the readonly attribute on textbox inside the three divs?

$('.edit').click(function(){

$('.first_div input, .second_div input, .third_div input').removeAttr('readonly');

});



$('.edit_first').click(function(){

$('#first_textbox').removeAttr('readonly');

});

$('.edit_second').click(function(){

$('#second_textbox').removeAttr('readonly');

});

$('.edit_third').click(function(){

$('#third_textbox').removeAttr('readonly');

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div >
<input readonly id="first_textbox">
</div>
<div >
<input readonly id="second_textbox">
</div>
<div >
<input readonly id="third_textbox">
</div>
<button >Edit All</button>
<button >Edit first textbox</button>
<button >Edit second textbox</button>
<button >Edit third textbox</button>

  • Related