I am trying to get the list id from a different element when a form is submitted so that I can associate the list to the 'todo' item and create that 'todo' with the list id it is under. I keep getting the integrityError due to not having the list_id when form is submitted. So how do I get the data-id from the other element? I have tried document.querySelectorAll('lists') but nothing is coming through it. Javscript for attempting to retrieve the data-id
'''
document.getElementById('form').onsubmit = function(e) {
e.preventDefault();
const desc = descInput.value;
const id1 = document.querySelectorAll('#lists');
const id = id1.dataset.id;
console.log(id);
fetch('/todos/create', {
method: 'POST',
body: JSON.stringify({
'description': desc,
'list_id' : id,
}),
headers: {
'Content-Type': 'application/json',
}
})
'''
element I am trying to get the id from
'''
<ul id='lists'>
{% for list in lists %}
<li><input class="list-completed" data-id='{{ list.id }}'
type="checkbox"
{% if list.completed %} checked {% endif %}>
<a href="/lists/{{ list.id }}">
{{ list.name }}
</a><button class="deletelist" data-id='{{ list.id }}'
type="button">✗</button>
</li>
{% endfor %}
</ul>
'''
form that will be submitted to create a 'todo' item
'''
<form id="form" method="post" action="/todos/create">
<input type="text" id="description" name="description" />
<input class="something" type="submit" value="Create" />
</form>
'''