5

HTML:

<button class="btn btn-sm green btn-outline filter-submit margin-bottom" data-type="Invoice Generate" onclick="show_modal()"><i class="fa fa-envelope"></i></button>

jQuery:

function show_modal(){
    alert($(this).data("type"));
    $('#basic').modal('show'); 
}

Output:

Undefined

Expected Output:

Invoice Generate

Anyone can please tell me why it alerting undefined and How can I resolve it?

2
  • For what purpose you are using modal? Commented Sep 12, 2017 at 12:10
  • @Ravigohil What does that matter? Commented Sep 12, 2017 at 12:11

4 Answers 4

5

You show_modal function don't know what $(this) is. so add this to onclick="show_modal()" like onclick="show_modal(this)"

Then you, of course, has to update your function as well as below:

function show_modal(obj){
    alert($(obj).data("type"));
    $('#basic').modal('show'); 
}

function show_modal(obj){
    alert($(obj).data("type"));
    $('#basic').modal('show'); 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btn btn-sm green btn-outline filter-submit margin-bottom" data-type="Invoice Generate" onclick="show_modal(this)"><i class="fa fa-envelope"></i></button>

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

4 Comments

Sir it working here but not working on my link can you please check it. ec2-52-66-146-251.ap-south-1.compute.amazonaws.com/Purvi/…
@RushabhShah You get the following error in your console jQuery is not defined means your jquery is not loaded correctly
Actually I forgot to add obj in this line show_modal(obj) now I added and it working fine thanks for your help.
@RushabhShah No problem, happy to help
0

Or if you want to use $(this) attach an event handler to the element using it's class or id like this..

$('.filter-submit').on('click', function(){
  alert($(this).data("type"));
})

$('.filter-submit').on('click', function() {
  alert($(this).data("type"));
  //$('#basic').modal('show');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button class="btn btn-sm green btn-outline filter-submit margin-bottom" data-type="Invoice Generate"><i class="fa fa-envelope"></i>Show</button>

Comments

0

you are calling function you have to pass this will function like

<button class="btn btn-sm green btn-outline filter-submit margin-bottom" data-type="Invoice Generate" onclick="show_modal()"><i class="fa fa-envelope"></i></button>

And in JS

function show_modal(ob){
    alert($(ob).data("type"));
    $('#basic').modal('show'); 
}

3 Comments

using this in show_modal(this) will not work, since this is a reserved word.
Your still missing to add something to show_modal()
if #basic is your model then you have to use $('#basic').show() instead of $('#basic').modal('show');
0

Hope this is your solution

<button class="btn btn-sm green btn-outline filter-submit margin-bottom" data-type="Invoice Generate" onclick="show_modal(this)"><i class="fa fa-envelope"></i></button>

<script>
   function show_modal(ele){

      alert($(ele).data("type"));
      $('#basic').modal('show'); 
   }
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.