Hi, I need help with the code below. I want to check if the bookmark value exists in the array. Please scan through the code and find the bold one. It didnโt give me the right output. Thank you.
//save bookmark
function saveBookmark(e){
//Get form values
var siteName = document.getElementById('siteName').value;
var siteUrl = document.getElementById('siteUrl').value;
if(!validateForm(siteName, siteUrl)){
return false;
}
var bookmark = {
name: siteName,
url: siteUrl
}
//test if bookmarks is null
if(localStorage.getItem('bookmarks') === null){
//initialize an array
var bookmarks = [];
//add bookmark into the array
bookmarks.push(bookmark);
//set to localStorage
localStorage.setItem('bookmarks', JSON.stringify(bookmarks));
} else{
//get bookmarks from localStorage
var bookmarks = JSON.parse(localStorage.getItem('bookmarks'));
//add bookmark into array
bookmarks.push(bookmark);
๐ฏ๐๐ซ ๐๐จ๐ฎ๐ง๐ = ๐๐จ๐จ๐ค๐ฆ๐๐ซ๐ค๐ฌ.๐ฌ๐จ๐ฆ๐(๐๐ฎ๐ง๐๐ญ๐ข๐จ๐ง(๐๐ฅ){
๐ซ๐๐ญ๐ฎ๐ซ๐ง ๐๐ฅ.๐ง๐๐ฆ๐ === ๐๐จ๐จ๐ค๐ฆ๐๐ซ๐ค๐ฌ.๐ฌ๐ข๐ญ๐๐๐๐ฆ๐;
});
๐๐จ๐ง๐ฌ๐จ๐ฅ๐.๐ฅ๐จ๐ (๐๐จ๐ฎ๐ง๐);
//re-set back to localStorage
localStorage.setItem('bookmarks', JSON.stringify(bookmarks));
//clear form
document.getElementById('myForm').reset();
//re-fetch bookmarks
fetchBookmarks();
}
//Prevent form from submitting
e.preventDefault();
}
first, learn to indent your code!
Maybe the name is a different type, but either way, Iโd just run this in the console step by step to see why it isnโt found.
1 Like
The problem is with this line:
return el.name === bookmarks.siteName;
bookmarks
is an array, not an object, so it wonโt have the key siteName
. Also the bookmark
object you previously create doesnโt have the siteName
key either. You need to use a valid object with a valid key for your code to work.
1 Like
So what should I do? I have kept debugging it for 1-2 hours.
Hereโs the complete code for this.
Need much help. Thanks.
The problem is on this line:
if(localStorage.getItem('bookmarks') === null)
If 'bookmarks'
is not present, localStorage.getItem('bookmarks')
will return undefined
, not null
.
$(document).ready(function(){
$("button").click(function(){
var str1=JSON.parse(localStorage.bookmarks);
var namex = document.getElementById("siteName").value;
var urlx = document.getElementById("siteUrl").value;
for(var i=0;i<str1.length;i++){
if(namex===str1[i].name){
alert("Show me duplicates name indexes: "+i);//Value for name entered already exists
}
if(urlx===str1[i].url){
alert("Show me duplicates url indexes: "+i);//Value for url entered already exists
}
}
});
});
When you click your button ( อกยฐ อส อกยฐ) โฆ
- You parse json string into json itself.
- get values of fields for name and url.
- Then iterate though array of objects whom previously are parse into json from string.
- Conditions are self explanatory.
1 Like
Thank you guys, I have finally solved it!!!
I realized I missed out the variable declared, which is
var siteName = document.getElementById('siteName').value;
Thanks to @codename11
You can check out the code here https://codepen.io/SparklePink/pen/LJGzMO and test it.
Thanks to this wonderful site: https://stackoverflow.com/a/22844712/10120050
And also thank you @joops75 for helping out. I appreciate it.
Any related comments are welcomed
You should disable adding duplicates. Before adding, check if is there already bookmark under same name, url or both in localStorage. If there is, let message pop out โYou already have that bookmarked!โ or somethinโ โฆ
1 Like