11/////////////////// getting the note container/////////////////
22main_container = document . querySelector ( ".note_container" ) ;
3-
3+ var global_note_id = 0 ;
44////////////////making storenote as a default and unchangeble option/////////////////
55if ( document . getElementById ( "storenote" ) )
66{
@@ -122,6 +122,7 @@ function data_validator() {
122122 error_message_shower ( "" , "Note Data Can't be Empty" ) ;
123123 }
124124 else {
125+
125126 if ( unique_notes_verifier ( ) )
126127 {
127128 add_note ( title_el , data_el ) ;
@@ -132,6 +133,19 @@ function data_validator() {
132133}
133134//////////////add note fuction ////////////////////////
134135function add_note ( title_el , data_el ) {
136+
137+ if ( global_note_id != 0 )
138+ {
139+ note_body = document . getElementById ( global_note_id ) . children [ 0 ] . children [ 1 ] ;
140+ note_body . children [ 0 ] . innerHTML = title_el ;
141+ note_body . children [ 1 ] . innerHTML = data_el . slice ( 0 , 30 ) + "..." ;
142+ global_note_id = 0 ;
143+
144+ }
145+ else
146+ {
147+
148+
135149 notes_count = document . querySelectorAll ( ".note" ) . length ; // returns total created notes
136150
137151 note_card = document . createElement ( "div" ) ;
@@ -159,15 +173,19 @@ function add_note(title_el, data_el) {
159173 </div>` ;
160174
161175 document . getElementById ( `${ notes_count + 1 } ` ) . innerHTML = html ; //adding the html for the proper view
176+ }
162177
163178}
164179
165180
166181//////////////////////the delete function. This will remove note from dom and also from localstorage if the note is previously saved in local storage ////////////////////
167- function notedelete ( id , title , msg = "" ) {
168- el = document . getElementById ( id ) ;
182+ function notedelete ( id = 0 , title , msg = "" ) {
183+ if ( id != 0 )
184+ {
185+ el = document . getElementById ( id ) ;
186+ el . remove ( ) ;
187+ }
169188
170- el . remove ( ) ;
171189 let notes_array = JSON . parse ( localStorage . getItem ( "notes" ) ) ;
172190 temp = notes_array ;
173191 temp . forEach ( function key_popper ( ab , ind ) {
@@ -231,41 +249,44 @@ function unique_notes_verifier() {
231249
232250
233251 let noteselm = JSON . parse ( localStorage . getItem ( "notes" ) ) ;
234- try
252+ if ( noteselm )
235253 {
236- noteselm . forEach ( function note_data_extractor ( note , ind ) {
237- for ( var title in note )
238- {
254+ try
255+ {
256+ noteselm . forEach ( function note_data_extractor ( note , ind ) {
257+ for ( var title in note )
258+ {
259+
260+ if ( title . toLowerCase ( ) == input . toLowerCase ( ) )
261+ {
262+
263+ error_message_shower ( "Title Already Exist" ) ;
264+ throw "Title Already Exist"
239265
240- if ( title . toLowerCase ( ) == input . toLowerCase ( ) )
241- {
242-
243- error_message_shower ( "Title Already Exist" ) ;
244- throw "Title Already Exist"
245266
246-
247- }
248- }
249- } ) ;
250- terror = document . getElementById ( "title_error" ) ;
251-
252- terror . innerHTML = "" ;
253- terror . setAttribute ( "style" , "color: red; font-size: 12px;" ) ;
254- t_el = document
255- . getElementById ( "note_title" )
256- . setAttribute ( "style" , "border: 1px solid green;" ) ;
257- document
258- . getElementById ( "note_data" )
259- . setAttribute ( "style" , "border: 1px solid green;" ) ;
260-
261- return true ;
262- }
263- catch ( err )
264- {
265- return false ;
267+ }
268+ }
269+ } ) ;
270+ terror = document . getElementById ( "title_error" ) ;
271+
272+ terror . innerHTML = "" ;
273+ terror . setAttribute ( "style" , "color: red; font-size: 12px;" ) ;
274+ t_el = document
275+ . getElementById ( "note_title" )
276+ . setAttribute ( "style" , "border: 1px solid green;" ) ;
277+ document
278+ . getElementById ( "note_data" )
279+ . setAttribute ( "style" , "border: 1px solid green;" ) ;
280+
281+ return true ;
282+ }
283+ catch ( err )
284+ {
285+ return false ;
286+ }
266287 }
267-
268-
288+ else
289+ return true ;
269290}
270291//////////////////this will clear your local storage////////////////////
271292
@@ -320,6 +341,8 @@ function note_search(e) {
320341
321342function edit_note ( id )
322343{
344+ global_note_id = id ;
345+
323346 ntitle = document . getElementById ( "note_title" ) ;
324347
325348 data = document . getElementById ( "note_data" ) ;
@@ -333,11 +356,10 @@ function edit_note(id)
333356 notesObj . forEach ( function note_data_extractor ( note , ind ) {
334357 for ( var title in note ) {
335358
336- if ( ntitle . value == title ) {
337- ndata = note [ title ] ;
338- console . log ( ndata )
339- throw "found" ;
340-
359+ if ( ntitle . value == title )
360+ {
361+ ndata = note [ title ] ;
362+ throw "found" ;
341363 }
342364 }
343365 } ) ;
@@ -351,7 +373,7 @@ function edit_note(id)
351373
352374 msg = "Note is in edit mode now. Don't left without saving this note otherwise it will be lost."
353375
354- notedelete ( id , note . children [ 0 ] . innerText , msg ) ;
376+ notedelete ( 0 , note . children [ 0 ] . innerText , msg ) ;
355377
356378}
357379
0 commit comments