I want to pass string variable from one jquery event to another jquery event. I searched some problems and solutions on stack overflow regarding my issue, but I couldn't find one relevant solution with respect to my problem which I am facing. I am performing some backend operations using Jquery and AJAX using python Flask and Neo4j graph database.
The following is my jquery code
<script type="text/javascript">
var asstName = "";
$('select#label_Name').change(function(){
asstName = $('#label_Name').val();
console.log(asstName);
$.ajax({
url: '/get_asset',
data: asstName,
type: 'POST',
dataType: 'json',
}).done(function(data){
// console.log(data);
var print = "";
for (i=0; i < data['nodeList'].length; i++){
print += "<tr>" +
"<td>" + data['nodeList'][i]['Asset_Name'] + "</td>" +
"<td>" + data['nodeList'][i]['Asset_Manufacturer'] + "</td>" +
"<td><button id=" + data['nodeList'][i]['Asset_Name'] + ">" +
"<i style='color: red;' class='fa fa-trash'></i></button>" +
" " +
"<button id=" + data['nodeList'][i]['Asset_Name'] + ">" +
"<i style='color: blue;' class='fa fa-pencil'></i></button>" +
"</td>" +
"</tr>";
}
$('tbody#tBody').html(print);
})
});
$('#button').click(function(){
var delName = $(this).attr('id');
console.log(asstName);
$.ajax({
url: '/deleteNode',
data: JSON.stringify(delName),
dataType: 'json',
type: 'POST'
}).done(function(data){
console.log(data);
})
});
</script>
- There are two events one is "drop-down on change" list and other one is button click.
- When I select any list element from drop-down ajax call is made and from data base data is populated in table.
- Now I am performing some operation on table elements where I have used buttons for Deleting element.
As you can see there are two events that is
$('select#label_Name').change
and
$('#button').click
How can I pass string variable "asstName" to event button click?