How to retrieve saved id then pass as selected test in jquery












0















i have a modal form where user enters details like for an item,the modal has a select option where user can select the item category there are savings,i have no problem here.the issue is when the user want to edit some details of the item.u will require to retrieve the item.here is my issue i cant get the category saved transferred back to the select option then set is as selected.
how can i do this.//id of selected category is the one that is inserted to db.



$.get(
'../Inventory/PopulateCategory',
{},
function (data) {
$('#Category').empty();

$.each(data,
function (key, value) {
$('#Category')
.append($("<option />")
.val(value.StockCategoryId)
.html(value.StockCategory));
});
},
'json'
);


this is how i retrieve the value,am able to load input but cant get to load the correct saved item to select.



function EditCompanyRecord(_id) {
var url = "/Inventory/itemEdit?_id=" + _id;
$.ajax({
type: "GET",
url: url,
dataType: "json",
success: function (data) {
var obj = JSON.parse(data);
var currcat = obj.StockCategory;
var _vat = obj.taxcode;
$("#StockId").val(_id);
$("#Description").val(obj.description);
$('#Category').val(obj.StockCategory)
//i need to reverse and get save id as the selected category
}
});
}









share|improve this question

























  • your question is not clear(Complex sentence formation). Can you reframe the sentence with your apps actual scenario you are trying to implement using simple sentence.

    – Pranesh Janarthanan
    Nov 23 '18 at 13:58













  • Don't add value to the #Category. Category is a select. You want to populate each <option> with all the different StockCategory (s).

    – Persijn
    Nov 23 '18 at 15:12


















0















i have a modal form where user enters details like for an item,the modal has a select option where user can select the item category there are savings,i have no problem here.the issue is when the user want to edit some details of the item.u will require to retrieve the item.here is my issue i cant get the category saved transferred back to the select option then set is as selected.
how can i do this.//id of selected category is the one that is inserted to db.



$.get(
'../Inventory/PopulateCategory',
{},
function (data) {
$('#Category').empty();

$.each(data,
function (key, value) {
$('#Category')
.append($("<option />")
.val(value.StockCategoryId)
.html(value.StockCategory));
});
},
'json'
);


this is how i retrieve the value,am able to load input but cant get to load the correct saved item to select.



function EditCompanyRecord(_id) {
var url = "/Inventory/itemEdit?_id=" + _id;
$.ajax({
type: "GET",
url: url,
dataType: "json",
success: function (data) {
var obj = JSON.parse(data);
var currcat = obj.StockCategory;
var _vat = obj.taxcode;
$("#StockId").val(_id);
$("#Description").val(obj.description);
$('#Category').val(obj.StockCategory)
//i need to reverse and get save id as the selected category
}
});
}









share|improve this question

























  • your question is not clear(Complex sentence formation). Can you reframe the sentence with your apps actual scenario you are trying to implement using simple sentence.

    – Pranesh Janarthanan
    Nov 23 '18 at 13:58













  • Don't add value to the #Category. Category is a select. You want to populate each <option> with all the different StockCategory (s).

    – Persijn
    Nov 23 '18 at 15:12
















0












0








0








i have a modal form where user enters details like for an item,the modal has a select option where user can select the item category there are savings,i have no problem here.the issue is when the user want to edit some details of the item.u will require to retrieve the item.here is my issue i cant get the category saved transferred back to the select option then set is as selected.
how can i do this.//id of selected category is the one that is inserted to db.



$.get(
'../Inventory/PopulateCategory',
{},
function (data) {
$('#Category').empty();

$.each(data,
function (key, value) {
$('#Category')
.append($("<option />")
.val(value.StockCategoryId)
.html(value.StockCategory));
});
},
'json'
);


this is how i retrieve the value,am able to load input but cant get to load the correct saved item to select.



function EditCompanyRecord(_id) {
var url = "/Inventory/itemEdit?_id=" + _id;
$.ajax({
type: "GET",
url: url,
dataType: "json",
success: function (data) {
var obj = JSON.parse(data);
var currcat = obj.StockCategory;
var _vat = obj.taxcode;
$("#StockId").val(_id);
$("#Description").val(obj.description);
$('#Category').val(obj.StockCategory)
//i need to reverse and get save id as the selected category
}
});
}









share|improve this question
















i have a modal form where user enters details like for an item,the modal has a select option where user can select the item category there are savings,i have no problem here.the issue is when the user want to edit some details of the item.u will require to retrieve the item.here is my issue i cant get the category saved transferred back to the select option then set is as selected.
how can i do this.//id of selected category is the one that is inserted to db.



$.get(
'../Inventory/PopulateCategory',
{},
function (data) {
$('#Category').empty();

$.each(data,
function (key, value) {
$('#Category')
.append($("<option />")
.val(value.StockCategoryId)
.html(value.StockCategory));
});
},
'json'
);


this is how i retrieve the value,am able to load input but cant get to load the correct saved item to select.



function EditCompanyRecord(_id) {
var url = "/Inventory/itemEdit?_id=" + _id;
$.ajax({
type: "GET",
url: url,
dataType: "json",
success: function (data) {
var obj = JSON.parse(data);
var currcat = obj.StockCategory;
var _vat = obj.taxcode;
$("#StockId").val(_id);
$("#Description").val(obj.description);
$('#Category').val(obj.StockCategory)
//i need to reverse and get save id as the selected category
}
});
}






javascript jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 14:08







bita pinches

















asked Nov 23 '18 at 13:48









bita pinchesbita pinches

32




32













  • your question is not clear(Complex sentence formation). Can you reframe the sentence with your apps actual scenario you are trying to implement using simple sentence.

    – Pranesh Janarthanan
    Nov 23 '18 at 13:58













  • Don't add value to the #Category. Category is a select. You want to populate each <option> with all the different StockCategory (s).

    – Persijn
    Nov 23 '18 at 15:12





















  • your question is not clear(Complex sentence formation). Can you reframe the sentence with your apps actual scenario you are trying to implement using simple sentence.

    – Pranesh Janarthanan
    Nov 23 '18 at 13:58













  • Don't add value to the #Category. Category is a select. You want to populate each <option> with all the different StockCategory (s).

    – Persijn
    Nov 23 '18 at 15:12



















your question is not clear(Complex sentence formation). Can you reframe the sentence with your apps actual scenario you are trying to implement using simple sentence.

– Pranesh Janarthanan
Nov 23 '18 at 13:58







your question is not clear(Complex sentence formation). Can you reframe the sentence with your apps actual scenario you are trying to implement using simple sentence.

– Pranesh Janarthanan
Nov 23 '18 at 13:58















Don't add value to the #Category. Category is a select. You want to populate each <option> with all the different StockCategory (s).

– Persijn
Nov 23 '18 at 15:12







Don't add value to the #Category. Category is a select. You want to populate each <option> with all the different StockCategory (s).

– Persijn
Nov 23 '18 at 15:12














1 Answer
1






active

oldest

votes


















0














The value persisted as category for the stock is the category name/title.
While the options input uses the category id for the value.



In order to have persisted category for the stock selected in the category options, you need to find the id for the persisted category for the stock in the category options. e.g.



const $objStockCategoryId = $('#Category').children().filter((i, e) => 
e.text === obj.StockCategory);


Then select the category option



$('#Category').val($objStockCategoryId.val());


However, I suggest to set a data attribute in the category options for a succinct DOM query. e.g.



$.each(data,
function (key, value) {
$('#Category')
.append(
$("<option />")
.attr('data-name', value.StockCategory)
.val(value.StockCategoryId)
.html(value.StockCategory)
);
});

const $objStockCategoryId = $('#Category')
.find(`[data-name="${obj.StockCategory}"]`);

$('#Category').val($objStockCategoryId.val());





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447904%2fhow-to-retrieve-saved-id-then-pass-as-selected-test-in-jquery%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The value persisted as category for the stock is the category name/title.
    While the options input uses the category id for the value.



    In order to have persisted category for the stock selected in the category options, you need to find the id for the persisted category for the stock in the category options. e.g.



    const $objStockCategoryId = $('#Category').children().filter((i, e) => 
    e.text === obj.StockCategory);


    Then select the category option



    $('#Category').val($objStockCategoryId.val());


    However, I suggest to set a data attribute in the category options for a succinct DOM query. e.g.



    $.each(data,
    function (key, value) {
    $('#Category')
    .append(
    $("<option />")
    .attr('data-name', value.StockCategory)
    .val(value.StockCategoryId)
    .html(value.StockCategory)
    );
    });

    const $objStockCategoryId = $('#Category')
    .find(`[data-name="${obj.StockCategory}"]`);

    $('#Category').val($objStockCategoryId.val());





    share|improve this answer




























      0














      The value persisted as category for the stock is the category name/title.
      While the options input uses the category id for the value.



      In order to have persisted category for the stock selected in the category options, you need to find the id for the persisted category for the stock in the category options. e.g.



      const $objStockCategoryId = $('#Category').children().filter((i, e) => 
      e.text === obj.StockCategory);


      Then select the category option



      $('#Category').val($objStockCategoryId.val());


      However, I suggest to set a data attribute in the category options for a succinct DOM query. e.g.



      $.each(data,
      function (key, value) {
      $('#Category')
      .append(
      $("<option />")
      .attr('data-name', value.StockCategory)
      .val(value.StockCategoryId)
      .html(value.StockCategory)
      );
      });

      const $objStockCategoryId = $('#Category')
      .find(`[data-name="${obj.StockCategory}"]`);

      $('#Category').val($objStockCategoryId.val());





      share|improve this answer


























        0












        0








        0







        The value persisted as category for the stock is the category name/title.
        While the options input uses the category id for the value.



        In order to have persisted category for the stock selected in the category options, you need to find the id for the persisted category for the stock in the category options. e.g.



        const $objStockCategoryId = $('#Category').children().filter((i, e) => 
        e.text === obj.StockCategory);


        Then select the category option



        $('#Category').val($objStockCategoryId.val());


        However, I suggest to set a data attribute in the category options for a succinct DOM query. e.g.



        $.each(data,
        function (key, value) {
        $('#Category')
        .append(
        $("<option />")
        .attr('data-name', value.StockCategory)
        .val(value.StockCategoryId)
        .html(value.StockCategory)
        );
        });

        const $objStockCategoryId = $('#Category')
        .find(`[data-name="${obj.StockCategory}"]`);

        $('#Category').val($objStockCategoryId.val());





        share|improve this answer













        The value persisted as category for the stock is the category name/title.
        While the options input uses the category id for the value.



        In order to have persisted category for the stock selected in the category options, you need to find the id for the persisted category for the stock in the category options. e.g.



        const $objStockCategoryId = $('#Category').children().filter((i, e) => 
        e.text === obj.StockCategory);


        Then select the category option



        $('#Category').val($objStockCategoryId.val());


        However, I suggest to set a data attribute in the category options for a succinct DOM query. e.g.



        $.each(data,
        function (key, value) {
        $('#Category')
        .append(
        $("<option />")
        .attr('data-name', value.StockCategory)
        .val(value.StockCategoryId)
        .html(value.StockCategory)
        );
        });

        const $objStockCategoryId = $('#Category')
        .find(`[data-name="${obj.StockCategory}"]`);

        $('#Category').val($objStockCategoryId.val());






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 19:30









        Oluwafemi SuleOluwafemi Sule

        11.8k1532




        11.8k1532
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447904%2fhow-to-retrieve-saved-id-then-pass-as-selected-test-in-jquery%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Tonle Sap (See)

            I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

            Guatemaltekische Davis-Cup-Mannschaft