'Search button not working properly. an error shows on console. The error is " element.getElementsByTagName(...)[0] is undefined ". solve it. I can't
This is a note app where user can add their important notes. All codes are here right but Search button doesn't work. When I click search bar and type some text for finding my notes an error shows in the console area which is " element.getElementsByTagName(...)[0] is undefined " . This is the problem of my code. I can't understand why this shows an error. what's wrong with this code.
<-- This is HTML Code -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Notes App</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">Magic Notes</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input id="searchTxt" class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
<div class="container my-3">
<h1>Welcome To Magic Notes</h1>
<div class="card">
<div class="card-body">
<h5 class="card-title">Add a note</h5>
<div class="form-group">
<textarea class="form-control" id="addTxt" rows="3"></textarea>
</div>
<button class="btn btn-primary" id="addBtn">Add Note</button>
</div>
</div>
<hr>
<h1>Your Notes</h1>
<hr>
<div id="notes" class="row container-fluid"> </div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>
<script src="js/app.js"></script>
</body>
</html>
// This is Javascript code.
let addBtn = document.getElementById('addBtn');
addBtn.addEventListener('click', function(e){
let addTxt = document.getElementById('addTxt');
let notes = localStorage.getItem('notes');
if ( notes == null ){
notesObj = [];
}
else{
notesObj = JSON.parse(notes);
}
notesObj.push(addTxt.value);
localStorage.setItem('notes', JSON.stringify(notesObj));
addTxt.value = '';
console.log(notesObj);
showNotes();
})
function showNotes(){
let notes = localStorage.getItem('notes');
if ( notes == null ){
notesObj = [];
}
else{
notesObj = JSON.parse(notes);
}
let html = "";
notesObj.forEach(function(element, index){
{
html += `
<div class="card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">${index + 1}</h5>
<p class="card-text">${element}</p>
<button id="${index}" onclick = "deleteNotes(this.id)" class="btn btn-primary">Delete Note</button>
</div>
</div>`;
};
});
let notesElement = document.getElementById('notes');
if (notesObj.length != 0){
notesElement.innerHTML = html;
}
else{
notesElement.innerHTML = `Nothing to show!</br>
Use this 'Note App' to add your note`;
}
};
showNotes();
function deleteNotes(index){
//console.log('I am Deleting', index);
let notes = localStorage.getItem('notes');
if ( notes == null ){
notesObj = [];
}
else{
notesObj = JSON.parse(notes);
}
notesObj.splice(index, 1);
localStorage.setItem('notes', JSON.stringify(notesObj));
showNotes();
}
let search = document.getElementById('searchTxt');
search.addEventListener("input", function(){
let inputVal = search.value.toLowerCase();
//console.log('Input event fired!', inputVal);
let card = document.getElementsByClassName('card');
Array.from(card).forEach(function(element){
****let cardTxt = element.getElementsByTagName("p")[0].innerText;****`enter code here`
if ( cardTxt.includes(inputVal)){
element.style.display = "block";`enter code here`
}
else {
element.style.display = "none";
}
});
});
Solution 1:[1]
There error message suggests that element.getElementsByTagName("p") is returning an empty result, which is no suprise considering that element is an element with the class card and the HTML contains an element with the class card, which doesn't contain any p elements.
Solution 2:[2]
Hi you need to iterate over the array and replace the 999.
ids = [12,34,56,78]
data = '{"id":999}'
for i in ids:
print(data.replace('999',str(i)))
Solution 3:[3]
I don't understand your question completely, but I hope this code is helpful.
ids = [12,34,56,78]
data = '{"id":999}'
IDS = []
dataeval = eval(data)
for id in ids:
dataeval["id"] = id
IDS.append(str(dataeval))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | RoToRa |
| Solution 2 | AmilaMGunawardana |
| Solution 3 | H.asadi |
