This is a javascript meant to be a part of chrome extension. It should print out id when I press the add button.
What it does, though, is I have to press add TWICE to have the id appended where I want it to ( I use this instead of alert)
When debugging, it says that bookmarkBarID is undefined… I suppose this is the error but I dont know how to fix it. Please help
here is the whole code:
<!doctype html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
var bookmarkBarID;
function getBM(){
chrome.bookmarks.getTree(function(tree){
bookmarkBarID=tree[0].children[0];
});
}
function CreateNewItem()
{
$('#submit').click(function (){
getBM()
console.log(bookmarkBarID);
$('.bookmark').append(bookmarkBarID.id);
})
}
</script>
</head>
<body onload="CreateNewItem();" style="width:1200px">
<div class="bookmark"></div>
<input type="text" value="enter url here" id = 'urlLink' />
<input type="text" value="enter Name title" id='linkName'/>
<input type="text" value="" id='folderName' />
<div><button id="submit">Add</button></div>
</body>
</html>
chrome.bookmarks.getTreeis an asynchronous function, which means that it runs at the same time as your other code. The function finishes after the rest of your code executes. To use the function’s results, you need to give a callback function that will be called with the results. In this case, you need to move$('.bookmark').append(bookmarkBarID.id);into the callback function:Note: you should use
$(document).ready(CreateNewItem);instead ofonload(assuming that you are using jQuery).