3

HTML CODE

<tbody> <tr> <td>0</td> <td>204093D-P12</td> <td>80443</td> <td>Name</td> <td><span class="label label-success">Updated</span></td> <td><button class="btn btn-xs btn-flat" data-toggle="modal" data-id="204093D-P132" data-target="#myModal" type="button" title="Add" onClick="ShowModal()"><i class="fa fa-plus" aria-hidden="true"></i></button> | <button class="btn btn-xs btn-flat" data-toggle="modal" data-id="204093D-P132" data-target="#myModal_edit" type="button" title="Edit" onClick="ShowEdit()"><i class="fa fa-pencil-square-o" aria-hidden="true" ></i></button>| <button class="btn btn-xs btn-flat" data-toggle="modal" data-id="204093D-P132" data-target="#myModal_details" type="button" title="Details" onClick="ShowDetails()"><i class="fa fa-list-ul" aria-hidden="true"></i></button></td> </tr><tr> <td>1</td> <td>216619D-P18</td> <td>16009</td> <td>Name</td> <td><span class="label label-success">Updated</span></td> <td><button class="btn btn-xs btn-flat" data-toggle="modal" data-id="216619D-P918" data-target="#myModal" type="button" title="Add" onClick="ShowModal()"><i class="fa fa-plus" aria-hidden="true"></i></button> | <button class="btn btn-xs btn-flat" data-toggle="modal" data-id="216619D-P918" data-target="#myModal_edit" type="button" title="Edit" onClick="ShowEdit()"><i class="fa fa-pencil-square-o" aria-hidden="true" ></i></button>| <button class="btn btn-xs btn-flat" data-toggle="modal" data-id="216619D-P918" data-target="#myModal_details" type="button" title="Details" onClick="ShowDetails()"><i class="fa fa-list-ul" aria-hidden="true"></i></button></td> </tr><tr> <td>2</td> <td>21663P0012</td> <td>13116</td> <td>Name</td> <td><span class="label label-success">Updated</span></td> <td><button class="btn btn-xs btn-flat" data-toggle="modal" data-id="216693P0012" data-target="#myModal" type="button" title="Add" onClick="ShowModal()"><i class="fa fa-plus" aria-hidden="true"></i></button> | <button class="btn btn-xs btn-flat" data-toggle="modal" data-id="216693P0012" data-target="#myModal_edit" type="button" title="Edit" onClick="ShowEdit()"><i class="fa fa-pencil-square-o" aria-hidden="true" ></i></button>| <button class="btn btn-xs btn-flat" data-toggle="modal" data-id="216693P0012" data-target="#myModal_details" type="button" title="Details" onClick="ShowDetails()"><i class="fa fa-list-ul" aria-hidden="true"></i></button></td> </tr><tr> <td>3</td> <td>217496D-P078</td> <td>16032</td> <td>Name</td> <td><span class="label label-success">Updated</span></td> <td><button class="btn btn-xs btn-flat" data-toggle="modal" data-id="217496D-P078" data-target="#myModal" type="button" title="Add" onClick="ShowModal()"><i class="fa fa-plus" aria-hidden="true"></i></button> | <button class="btn btn-xs btn-flat" data-toggle="modal" data-id="217496D-P078" data-target="#myModal_edit" type="button" title="Edit" onClick="ShowEdit()"><i class="fa fa-pencil-square-o" aria-hidden="true" ></i></button>| <button class="btn btn-xs btn-flat" data-toggle="modal" data-id="217496D-P078" data-target="#myModal_details" type="button" title="Details" onClick="ShowDetails()"><i class="fa fa-list-ul" aria-hidden="true"></i></button></td> </tr> </tbody> 

And i have to tried to get data-id attribute value from using Jquery in following way

function ShowModal(){ alert($(this).attr("data-id")); } 

but return undefinedhow to get data-id value from jquery? and i have an another doubt data-id value can hold numeric value or string value?

1

6 Answers 6

13

You need to pass the current element context in inline click handler like

<button onClick="ShowModal(this)" data-id="217496D-P078"></button> 

Then use the passed element reference to get the data-id. You can also use HTMLElement.dataset property like elem.dataset.id

function ShowModal(elem){ var dataId = $(elem).data("id"); alert(dataId); } 

Additionally, I would recommend you use to bind event handler's instead of ugly inline click handler.

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

Comments

3

One of the important part of jquery is manipulate the DOM.

DOM = Document Object Model : Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document.

It has several DOM Manipulation :

  1. Get Content : text(), html(), and val()
  2. Get Attributes : attr()

Now if u have to access a particular attribute on click of button or something else then use this :

so here is my HTML

<p><a href="http://www.google.com" id="test" data-id="id_12345">google.com</a></p> <button>Click Here</button> 

then u have to access the attributes of anchor tag like this:

$("button").click(function(){ alert($("#test").attr("href")); // output : http://www.google.com alert($("#test").attr("data-id")); // output : id_12345 }); 

1 Comment

Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this".
1

 $(function () { $(".inputs").click(function (e) { alert($(this).attr("data-id")); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="btn btn-xs btn-flat inputs" value="click" type="button" data-toggle="modal" data-id="217496D-P078" data-target="#myModal"/>

Comments

0

You can use Javascript's dataset or JQuqery data method:

$(document).ready(function() { $('button').on('click', function() { console.log(this.dataset.id, $(this).data().id, $(this).data('id')); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <tbody> <tr> <td> <button type="button" data-id="1">btn #1</button> <button type="button" data-id="2">btn #2</button> <br> </td> </tr> <tr> <td> <button type="button" data-id="3">btn #1</button> <button type="button" data-id="4">btn #2</button> <br> </td> </tr> <tr> <td> <button type="button" data-id="5">btn #1</button> <button type="button" data-id="6">btn# 2</button> <br> </td> </tr> <tr> <td> <button type="button" data-id="7">btn #1</button> <button type="button" data-id="8">btn #2</button> <br> </td> </tr> </tbody>

Comments

0

Your code is correct, all need to do is pass this to your function like:

<button class="btn btn-xs btn-flat" data-toggle="modal" data-id="204093D-P132" data-target="#myModal" type="button" title="Add" onClick="ShowModal(this)"><i class="fa fa-plus" aria-hidden="true"></i></button> 

Comments

0
You have to create unique data-id in your code data-id not unique. data-id="217496D-P078" data-target="#myModal" type="button" title="Add" data-id="217496D-P078" data-target="#myModal_edit" type="button" title=" data-id="217496D-P078" data-target="#myModal_details" type="button" titl then Try this <button onClick="ShowModal(this)" data-id="217496D-P078"></button> function ShowModal(elem){ var dataId = $(elem).data("id"); alert(dataId); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.