Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. How to read UI element value using jQuery (context vs find) ?

How to read UI element value using jQuery (context vs find) ?

Scheduled Pinned Locked Moved JavaScript
javascriptvisual-studiocomdesigntutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anjum Rizwi 0
    wrote on last edited by
    #1

    Do you have any preferences for below code code snippet? What approach do you follow when your site have too many fields. Option 1:

    PriorityOfAccessModel:
    {
    Id: $('#divPriorityAccess #Id').val(),
    FamilyId: $('#divkPriorityAccess #FamilyId').val(),
    ChildId: $('#divkPriorityAccess #ChildId').val(),
    Comment: $('#divkPriorityAccess #Comment').val(),
    CreatedBy: $('#divkPriorityAccess #CreatedBy').val(),
    CreatedOn: $('#divkPriorityAccess #CreatedOn').val(),
    Categories: accessPriority.getCategory(),
    Allocating: { Id: allocating }
    }

    Option 2 :

    var $x = $('#divPriorityAccess');

                    var accessModel =
                    {
                        Id: $x.find('#Id').val(),
                        FamilyId: $x.find('#FamilyId').val(),
                        ChildId: $x.find('#ChildId').val(),
                        Comment: $x.find('#Comment').val(),
                        CreatedBy: $x.find('#CreatedBy').val(),
                        CreatedOn: $x.find('#CreatedOn').val(),
                        Categories: accessPriority.getCategory(),
                        Allocating: { Id: allocating }
                    };
    

    Option 3:

    var $y = $('#divkPriorityAccess');
    var accessModel =
    {
    Id: $('#Id', $y).val(),
    FamilyId: $('#FamilyId', $y).val(),
    ChildId: $('#ChildId', $y).val(),
    Comment: $('#Comment', $y).val(),
    CreatedBy: $('#CreatedBy', $y).val(),
    CreatedOn: $('#CreatedOn', $y).val(),
    Categories: accessPriority.getCategory(),
    Allocating: { Id: allocating }
    };

    Context:

    Quote:

    $('.child', parentContext); Ops/Sec: 105,442 ±7.03% fastest

    Find:

    Quote:

    $(parentContext).find('.child'); Ops/Sec: 113,119 ±10.53% fastest

    My above finding is based on

    Quote:

    jQuery Selector Benchmark

    L Richard DeemingR 2 Replies Last reply
    0
    • A Anjum Rizwi 0

      Do you have any preferences for below code code snippet? What approach do you follow when your site have too many fields. Option 1:

      PriorityOfAccessModel:
      {
      Id: $('#divPriorityAccess #Id').val(),
      FamilyId: $('#divkPriorityAccess #FamilyId').val(),
      ChildId: $('#divkPriorityAccess #ChildId').val(),
      Comment: $('#divkPriorityAccess #Comment').val(),
      CreatedBy: $('#divkPriorityAccess #CreatedBy').val(),
      CreatedOn: $('#divkPriorityAccess #CreatedOn').val(),
      Categories: accessPriority.getCategory(),
      Allocating: { Id: allocating }
      }

      Option 2 :

      var $x = $('#divPriorityAccess');

                      var accessModel =
                      {
                          Id: $x.find('#Id').val(),
                          FamilyId: $x.find('#FamilyId').val(),
                          ChildId: $x.find('#ChildId').val(),
                          Comment: $x.find('#Comment').val(),
                          CreatedBy: $x.find('#CreatedBy').val(),
                          CreatedOn: $x.find('#CreatedOn').val(),
                          Categories: accessPriority.getCategory(),
                          Allocating: { Id: allocating }
                      };
      

      Option 3:

      var $y = $('#divkPriorityAccess');
      var accessModel =
      {
      Id: $('#Id', $y).val(),
      FamilyId: $('#FamilyId', $y).val(),
      ChildId: $('#ChildId', $y).val(),
      Comment: $('#Comment', $y).val(),
      CreatedBy: $('#CreatedBy', $y).val(),
      CreatedOn: $('#CreatedOn', $y).val(),
      Categories: accessPriority.getCategory(),
      Allocating: { Id: allocating }
      };

      Context:

      Quote:

      $('.child', parentContext); Ops/Sec: 105,442 ±7.03% fastest

      Find:

      Quote:

      $(parentContext).find('.child'); Ops/Sec: 113,119 ±10.53% fastest

      My above finding is based on

      Quote:

      jQuery Selector Benchmark

      L Offline
      L Offline
      Libin Emmanuel
      wrote on last edited by
      #2

      It's always better to specify the context. Especially when we have a heavy DOM.

      1 Reply Last reply
      0
      • A Anjum Rizwi 0

        Do you have any preferences for below code code snippet? What approach do you follow when your site have too many fields. Option 1:

        PriorityOfAccessModel:
        {
        Id: $('#divPriorityAccess #Id').val(),
        FamilyId: $('#divkPriorityAccess #FamilyId').val(),
        ChildId: $('#divkPriorityAccess #ChildId').val(),
        Comment: $('#divkPriorityAccess #Comment').val(),
        CreatedBy: $('#divkPriorityAccess #CreatedBy').val(),
        CreatedOn: $('#divkPriorityAccess #CreatedOn').val(),
        Categories: accessPriority.getCategory(),
        Allocating: { Id: allocating }
        }

        Option 2 :

        var $x = $('#divPriorityAccess');

                        var accessModel =
                        {
                            Id: $x.find('#Id').val(),
                            FamilyId: $x.find('#FamilyId').val(),
                            ChildId: $x.find('#ChildId').val(),
                            Comment: $x.find('#Comment').val(),
                            CreatedBy: $x.find('#CreatedBy').val(),
                            CreatedOn: $x.find('#CreatedOn').val(),
                            Categories: accessPriority.getCategory(),
                            Allocating: { Id: allocating }
                        };
        

        Option 3:

        var $y = $('#divkPriorityAccess');
        var accessModel =
        {
        Id: $('#Id', $y).val(),
        FamilyId: $('#FamilyId', $y).val(),
        ChildId: $('#ChildId', $y).val(),
        Comment: $('#Comment', $y).val(),
        CreatedBy: $('#CreatedBy', $y).val(),
        CreatedOn: $('#CreatedOn', $y).val(),
        Categories: accessPriority.getCategory(),
        Allocating: { Id: allocating }
        };

        Context:

        Quote:

        $('.child', parentContext); Ops/Sec: 105,442 ±7.03% fastest

        Find:

        Quote:

        $(parentContext).find('.child'); Ops/Sec: 113,119 ±10.53% fastest

        My above finding is based on

        Quote:

        jQuery Selector Benchmark

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        All three examples you have given are using ID selectors[^]. Since each ID within the document should be unique, there's no point specifying the ID of the parent element, using the .find method, or passing the parent element with the selector. With an ID selector, jQuery uses the document.getElementById function, which is extremely efficient. All of your examples will require additional checks, which will slow your code down. Also, by specifying the parent element's ID, your script is now tied to the layout of your document. Therefore, I would prefer option 4:

        PriorityOfAccessModel:
        {
        Id: $('#Id').val(),
        FamilyId: $('#FamilyId').val(),
        ChildId: $('#ChildId').val(),
        Comment: $('#Comment').val(),
        CreatedBy: $('#CreatedBy').val(),
        CreatedOn: $('#CreatedOn').val(),
        Categories: accessPriority.getCategory(),
        Allocating: { Id: allocating }
        }


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups