最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get the data-target dynamically - Stack Overflow

programmeradmin3浏览0评论

I have the follwing link:

<a href="edit/2" class="popup" data-toggle="modal" data-target="#productVariantModal"> <span class="glyphicon glyphicon-edit"></span></a>

I would like to get the data-target dynamically so I could reuse my javescript code.

I have tried something like this:

  var target = $(this).closest('[data-target]');

But this does't work.

Any Suggestions are wele.

Thanks.

I have the follwing link:

<a href="edit/2" class="popup" data-toggle="modal" data-target="#productVariantModal"> <span class="glyphicon glyphicon-edit"></span></a>

I would like to get the data-target dynamically so I could reuse my javescript code.

I have tried something like this:

  var target = $(this).closest('[data-target]');

But this does't work.

Any Suggestions are wele.

Thanks.

Share Improve this question edited Jun 30, 2015 at 11:52 Siguza 23.9k6 gold badges55 silver badges98 bronze badges asked May 11, 2014 at 9:24 sanderssanders 10.9k28 gold badges89 silver badges131 bronze badges 7
  • do you need the ID right ? – underscore Commented May 11, 2014 at 9:26
  • @samitha no. I want the value of data-target. – sanders Commented May 11, 2014 at 9:27
  • @sanders .attr('data-target') not working dynamically? – Artem Fitiskin Commented May 11, 2014 at 9:28
  • you wan to get on click of it?? – Ehsan Sajjad Commented May 11, 2014 at 9:28
  • What do you mean by "get the data-target"? Get the value of the data-target attribute? The contents of some DOM element with an ID set in the data-target attribute? Also curious about the meaning of "reuse my javascript code." – Nostalg.io Commented May 11, 2014 at 9:28
 |  Show 2 more ments

3 Answers 3

Reset to default 6

You need #productVariantModal right ? So.Simple use attr

var getValue = $('.popup').attr("data-target");

if it dynamically

var getValue = $(this).attr("data-target");

WORKING DEMO

var target = $(this).closest('[data-target]'); 

should be this:

var target =$('.popup').data('target');

Assuming that you are using this in the callback of some event fired by that hyperlink, to get the value of the data you can do:

$(this).data('target')

发布评论

评论列表(0)

  1. 暂无评论