How to get id in javascript from php to show data
Get the solution ↓↓↓this is my code
<textarea id="summernote<?php echo $u->id ?>" name="isi" placeholder="Tulis di sini" style="width: 100%; height: 100px !important; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;">
i put my javascript under textarea
<script type="text/javascript">
var id1="#summernote<?php echo $u->id ?>"
$(document).ready(function(){
$(id1).summernote({
height: "300px",
callbacks: {
onImageUpload: function(image) {
uploadImage(image[0]);
},
onMediaDelete : function(target) {
deleteImage(target[0].src);
}
}
});
function uploadImage(image) {
var data = new FormData();
data.append("image", image);
$.ajax({
url: "<?php echo site_url('trans/upload_image')?>",
cache: false,
contentType: false,
processData: false,
data: data,
type: "POST",
success: function(url) {
$(id1).summernote("insertImage", url);
},
error: function(data) {
console.log(data);
}
});
}
function deleteImage(src) {
$.ajax({
data: {src : src},
type: "POST",
url: "<?php echo site_url('trans/delete_image')?>",
cache: false,
success: function(response) {
console.log(response);
}
});
}
});
</script>
I want to show summernote with random id because when i just have one id in my textarea, it's just work for one. So I want to give it random id. But my code doesn't work, i don't know how to implement in javascript.
Answer
Solution:
Use a class instead and loop over all the elements in that class to initializesummerNote
instances. Also modify the functions as needed to pass in the editor element instance
HTML
<textarea class="editor" id="summernote<?php echo $u->id ?>" ></textarea>
JS (abbreviated)
$('.editor').each(function(){
// `this` is the current textarea element
const $textarea = $(this);
// init plugin for specific textarea instance
$textarea.summernote({
....
callbacks: {
onImageUpload: function(image) {
uploadImage($textarea, image[0]);
// ^^ which editor element
},
....
}
});
});
function uploadImage($testarea, image) {
// ^^ textarea object argument
.....
$.ajax({
.....
success: function(url) {
$testarea.summernote("insertImage", url);
// ^^ specific jQuery textarea instance
},
.....
});
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function store() on null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.