javascript - How to add click event tot button in string -


$.ajax({  		url:'/getarticles',  		method:'get',  	}).done(function(articles){  		var content = '';  		articles.foreach(function(e){  			var res = "<div class='article'>" +   						"<h3>" + e.title +  "</h3>" +   						"<p>" + e.content + "</p><br>" +   						"<button onclick=crud.remove(" + e._id + ")>remove</button><br>" +   					  "</div>";  			content += res;  		});  		$('#allarticles').append(content);  	});  	window.crud = (function(){  		// remove article  		function remove(id){  			console.log(id);  		}

how insert e._id correctly here put id of article?

when click button says:

(index):1 uncaught syntaxerror: invalid or unexpected token

there syntax error in button creation using jquery. missed single quotes id. missing braces.

<button onclick=crud.remove(" + e._id + ")>remove</button><br> 

replace above line this

<button onclick=crud.remove('" + e._id + "')>remove</button><br> 

i have corrected code :

$.ajax({  		url : '/getarticles',  		method : 'get',  	}).done(  			function(articles) {  				var content = '';  				articles.foreach(function(e) {  					var res = "<div class='article'>" + "<h3>" + e.title  							+ "</h3>" + "<p>" + e.content + "</p><br>"  							+ "<button onclick=crud.remove('" + e._id  							+ "')>remove</button><br>" + "</div>";  					content += res;  				});  				$('#allarticles').append(content);  			});  	window.crud = (function() {  		// remove article  		function remove(id) {  			console.log(id);  		}  	});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -