Search code examples
javascriptjquerytarget

get data-target and put into var with jquery


I guess it's a very basic question, but I really didn't get it to work.

Before opening a modal popup I'm reading out three data-targets with js.

var x = 2;
$('#confirm-delete').on('show.bs.modal', function(e) {
$(this).find('.btn-ok').attr('onclick', 'location.href=\'' + $(e.relatedTarget).data('href') + '\'');
$(this).find('input.timee').val($(e.relatedTarget).data('timess'));
$(this).find('input.prodtotext').val($(e.relatedTarget).data('prodid'));
$("#proddropchange").val(x.toString()).trigger('change');
});

This code works fine, but I want to remove the 4th line where I put the data-target "data-prodid" into the input "prodtotext". In return I want to read out the data-target "data-prodid" and put this value into "var x = 2"

With the value I'm setting the preselected value of a dropdown list which is different on every opened modal popup.

What I thought should work:

$('#confirm-delete').on('show.bs.modal', function(e) {
$(this).find('.btn-ok').attr('onclick', 'location.href=\'' + $(e.relatedTarget).data('href') + '\'');
$(this).find('input.timee').val($(e.relatedTarget).data('timess'));
$("#proddropchange").val(x.toString()).trigger('change');
var x = $(this).data("prodid");
});

Solution

  • Finally found my solution. Thank you for your advice.

    $('#confirm-delete').on('show.bs.modal', function(e) {
    $(this).find('.btn-ok').attr('onclick', 'location.href=\'' + $(e.relatedTarget).data('href') + '\'');
    $(this).find('input.timee').val($(e.relatedTarget).data('timess'));
    thisdata = $(e.relatedTarget).data('prodid');
    $("#proddropchange").val(thisdata.toString()).trigger('change');