user3772251
user3772251

Reputation: 77

How to get values from a button click event to another button click event

I want to get values from a div on button click event and get those values in another button click event.But not able to fetch those values in other click's event. here is code of my div:

'<div class="dvDynamic_' + pid + '"><span class="count_' + pid + '">' + count + '</span><span id="pname" style = "margin-left:70px;">' + pname + '</span><span id="punitprice" style = "margin-left:150px;">' + uprice + '</span></div>'

Code of First Button click event:

$(document).ready(function () {
    var ids = [];
    $("#btnproceed").click(function () {
        debugger;
        //   $("div[class^='apple-']")
        $("div[class^='dvDynamic_']").each(function () {
            debugger;
            var pids = $(this).text();
            ids.push(pids);
        });
        ids = [];
    });

Code of Second button click :

$('#btnsubmit').click(function () {
    debugger;
    var form = [{
        "name": "customerinfo",
        "value": JSON.stringify($('#customerform'))
    }];
    var data = JSON.stringify({
        'products': ids,
        'customerinfo': form
    });
    $.ajax({
        type: "POST",
        url: "@Url.Action("
        GetIds ", "
        Store ")",
        data: data,
        contentType: "application/json; charset=utf-8",
        success: function (r) {
            alert(r.d);
        }
    });
});

Upvotes: 1

Views: 334

Answers (1)

Grundy
Grundy

Reputation: 13381

now you clear array ids on each execute $("#btnproceed").click handler

$("#btnproceed").click(function () {
    debugger;
    //   $("div[class^='apple-']")
    $("div[class^='dvDynamic_']").each(function () {
        debugger;
        var pids = $(this).text();
        ids.push(pids);
    });
    ids = []; // here you clear all ids added in previous each loop
});

just remove this line ids = []; or move this to method start

$("#btnproceed").click(function () {
    ids = []; // here you clear all ids added in previous handler
    debugger;
    //   $("div[class^='apple-']")
    $("div[class^='dvDynamic_']").each(function () {
        debugger;
        var pids = $(this).text();
        ids.push(pids);
    });
    //before method exit - ids array contain data added in previous loop
});

Upvotes: 1

Related Questions