2

How can I pass this variable in javascript.

when this link is clicked I want to pass the post_slug in javascript.

<li data-toggle="modal" data-target="#myModal_<?php echo $post_slug;?>"><a>Edit</a></li>

In javascript I need the that variable with "imgInp" .

 $("#imgInp").change(function(){
    readURL(this);
});
// (imgInp<?php echo post_slug;?>)  how can I write it in js.    

1 Answer 1

1

To achieve this you could use another data attribute to hold the slug itself, then read it back and send it as a parameter to the function. Try this:

<li id="slug" data-toggle="modal" data-target="#myModal_<?php echo $post_slug; ?>" data-slug="imgInp<?php echo $post_slug; ?>">
  <a>Edit</a>
</li>
$("#imgInp").change(function() {
  var slug = $('#slug').data('slug');
  readURL(slug);
});

Note that I added the slug id to the li to make selecting it easier. You can use which ever method you prefer to identify the element.

Sign up to request clarification or add additional context in comments.

3 Comments

is the data-slug doing anything ??
if slug is "123" then i need "imgInp123"
You can append it: data-slug="imgInp<?php echo $post_slug; ?>"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.