Friday, March 24, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home JavaScript

Make Delete and Edit Buttons Work – JavaScript – SitePoint Forums

learningcode_x1mckf by learningcode_x1mckf
September 20, 2022
in JavaScript
0
Time limit for notify – JavaScript – SitePoint Forums
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


You might also like

Toolkit Allows JavaScript Devs to Program Embedded Devices – The New Stack

Select data value from grandparent div? – JavaScript – SitePoint

How to Handle Errors in JavaScript – Programming – MUO – MakeUseOf

Please assist me to complete my To Do App:

Add click on occasion on button “Take away” – In order that by click on on button “REMOVE” created merchandise could be faraway from localstorage as nicely.
Add click on occasion on button “Edit” – In order that created merchandise may very well be edited in localstorage as nicely.
Thank you numerous in your effort and time.
See my codepen

I dont know find out how to do it. Its not an project its simply my pet venture for myself. I began to study js 2 month in the past and its actually powerful for me. I used to be pondering that on this discussion board somebody might assist me by exhibiting how i could make them buttons work.


Gandalf



September 19, 2022, 3:32pm
#2

I’m certain somebody will likely be ready that will help you @dimerdelit however you solely posted an hour in the past.

Everybody on the boards is a volunteer, serving to others of their spare time, so please be affected person. :slight_smile:

1 Like

dimerdelit:

Add click on occasion on button “Take away” – In order that by click on on button “REMOVE” created merchandise could be faraway from localstorage as nicely.

I’m taking a look at this. WIll greater than seemingly be again to it within the morning my time as it’s a bit late right here.

On your info, you want a singular id for every todo. That approach once you click on on say ‘delete’ it is aware of which merchandise to take away from the todoList.

For delete you should use Array.filter to attain that e.g.

// will filter out the chosen merchandise based mostly on id
const updatedTodos = todoList.filter((todo) => todo.id !== idToDelete);

Okay I’ll attempt it and can let if i made it or not

Just a few concepts @dimerdelit,

I used to be having a play final night time, and it appears to me that making your add todo part right into a type is perhaps a greater method to go.

Right here is only a tough

<header>
    <div class="add-todo">
        <h1>ToDo checklist</h1>
        <type class="create_new_todo">
            <enter 
                sort="textual content" 
                id='input-task' 
                identify="input-task" 
                placeholder="Subsequent activity to do" 
                required
            >
            <button sort="submit">Add</button>
        </type>
    </div>
</header>

<major>
    <part class="todo-list-container">
        <ul class="todo-list">
            <li id='task-abcd1234'>
                <enter
                    sort="textual content" 
                    class="activity" 
                    identify="activity" 
                    worth="dummy activity 2" 
                    disabled
                >
                <button class="edit">Edit</button>
                <button class="delete">Delete</button>
            </li>
            <li id='task-dcbf3251'>
                <enter
                    sort="textual content" 
                    class="activity" 
                    identify="activity" 
                    worth="dummy activity 2" 
                    disabled
                >
                <button class="edit">Edit</button>
                <button class="delete">Delete</button>
            </li>
        </ul>
    </part>
</major>

You possibly can see that every activity is created as an enter that has been set to disabled. I’m pondering clicking on edit would take away that disabled property letting you edit the prevailing activity.

There’s additionally an id on the guardian checklist component, with the intention to edit or delete the proper activity from storage.

As I say simply concepts at this level.

Okay, however i’m extra frightened about js :smiley:
I up to date my code by the way in which.
Now I’m able to delete component, however not from localstorage and I add random id.
Thats all what i can. Different methods i simply dont perceive.

dimerdelit:

Okay, however i’m extra frightened about js :smiley:

The html is vital to the way you implement the JS. Should you get the html aspect of issues proper, it’s going to make the javascript that a lot less complicated.

As an illustration in your code you will have

addMessage.worth="";
addName.worth="";

Had these inputs been inside a type component, you possibly can simply reset the form.

addTodoForm.reset()

It’s your venture although, so simply meals for thought :slight_smile:

sure, I perceive and its true.
I simply need to end it in a approach I began.
Might you please assist me to delete component from localstorage utilizing my randon id quantity?

1 Like

dimerdelit:

Might you please assist me to delete component from localstorage utilizing my randon id quantity?

Do you will have an updated instance of your code?

Hello @dimerdelit, nicely I’ve tried to stay roughly to your code.

const addMessage = doc.querySelector('.message');
const addName = doc.querySelector('.identify');
const addButton = doc.querySelector('.add');
const todo = doc.querySelector('.todo');
const take away = doc.querySelector('.take away');

let todoList = [];

perform uniqueId() 
  return Math.random().toString(16).slice(2);


// a perform to replace the storage
// and replace the displayed checklist
perform updateTodoList (todoList) 
  localStorage.setItem('todo-list', JSON.stringify(todoList));
  displayMessages(todoList);


if (localStorage.getItem('todo-list')) 
   todoList = JSON.parse(localStorage.getItem('todo-list'));
   displayMessages(todoList);


addButton.addEventListener('click on', perform () );

// onClick="removeMessage('$todo.id')"
// passes the id to removeMessage
perform removeMessage (id) 
  // iterate by way of the prevailing todoList
  // and utilizing filter take away the todo that has an id
  // matching the todo we clicked delete on
  todoList = todoList.filter(
    perform(todo) 
      // solely return those that do not match
      return todo.id !== id;
    
  )
  
  updateTodoList(todoList);


perform displayMessages(todoList = []) 

  // Iterate by way of every todo with map.
  // For every todo return the checklist merchandise HTML
  const messages = todoList.map(
    perform(todo) 
      return `
      <li id="$todo.id">
        Identify:$todo.identify
        Message:$todo.message
        <button class="take away" onClick="removeMessage('$todo.id')">DELETE</button>
        <button class="edit">EDIT</button>
      </li>
      `;
    
  );
  
  // be a part of the array gadgets into one large string
  // utilizing a newline as a separator.
  todo.innerHTML = messages.be a part of('n');

Here’s a codepen. You’ll clearly have to work in your css/html.

Features that I’ve used, that are price .

developer.mozilla.org

Array.prototype.filter() – JavaScript | MDN

The filter() methodology creates a shallow copy of a portion of a given array, filtered down to simply the weather from the given array that cross the check carried out by the supplied perform.

developer.mozilla.org

Array.prototype.map() – JavaScript | MDN

The map() methodology creates
a brand new array populated with the outcomes of calling a supplied perform on
each component within the calling array.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join

Word: Personally I’d make additional adjustments to this, however didn’t need to veer too far off out of your authentic code.

Edit: Simply made a change to your inline onClick handler. Now passes the id to removeMessage as an alternative. I’d personally go for addEventListener, however this can be a less complicated resolution to passing the button component.



Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

Toolkit Allows JavaScript Devs to Program Embedded Devices – The New Stack

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Toolkit Allows JavaScript Devs to Program Embedded Devices  The New Stack Source link

Read more

Select data value from grandparent div? – JavaScript – SitePoint

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Select data value from grandparent div? - JavaScript  SitePoint Source link

Read more

How to Handle Errors in JavaScript – Programming – MUO – MakeUseOf

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

How to Handle Errors in JavaScript - Programming  MUO - MakeUseOf Source link

Read more

How to Use the Javascript Slice Method – hackernoon.com

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

How to Use the Javascript Slice Method  hackernoon.com Source link

Read more

Clean Code in JavaScript – SitePoint

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Clean Code in JavaScript  SitePoint Source link

Read more
Next Post
Java 19 Continues Evolution of Open Source Programming Language

Java 19 Continues Evolution of Open Source Programming Language

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

More than 250 newspaper sites across the US access malicious JavaScript in malware supply-chain attack

More than 250 newspaper sites across the US access malicious JavaScript in malware supply-chain attack

November 3, 2022
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Learn the Popular Programming Languages C and C++ for Just $5 a … – Entrepreneur

February 3, 2023
Fall Foliage: JavaScript/OJS Edition – Security Boulevard

Path traversal in Java web applications – announcing the Invicti technical paper

November 4, 2022

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • Java Developer Survey Reveals Increased Need for Java … – PR Newswire
  • What You Should Definitely Pay Attention to When Hiring Java Developers – Modern Diplomacy
  • Java Web Frameworks Software Market Research Report 2023 … – Los Alamos Monitor

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?