On click h2 I need this variable $post_id in this two places in javascript like I have added with img and input in html.
How can I get this variable in javascript and add with those two ids ?
<h2 id="view" data-target="#Modal_<?php echo $post_id;?>"> view </h2>
<img id="pic<?php echo $post_id; ?> " />
<input id="input<?php echo $post_id; ?>" >
js:
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#pic').attr('src', e.target.result); // here with #pic
}
reader.readAsDataURL(input.files[0]);
}
}
$("#input").change(function(){ // here with #input
readURL(this);
});
UPDATE:
when li is clicked change the ids.
<li data-toggle="modal" data-target="#myModal_<?php echo $post_slug;?>"><a>View</a></li>
<div id="myModal_<?php echo $post_slug;?>" >
<input type="file" class="file" id="imgInp<?php echo $post_slug;?>"/>
<img id="blah<?php echo $post_slug;?>" />
</div>
js:
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result); // here in id add this blah<?php echo $post_slug;?>
}
reader.readAsDataURL(input.files[0]);
}
}
$("#imgInp").change(function(){ // here in id add this imgInp<?php echo $post_slug;?>
readURL(this);
});
$('#view').data('target')