locked
FInd control in partial vew RRS feed

  • Question

  • User-2132746437 posted

    I have a main page [index.cshtml] that uses a partial view [ _details.cshtml ], in that page, _details.cshtml that builds controls on it dynamically. When the page renders, I have to apply styles to the controls, so if it's a drop down, it gets a certain style, textbox, different styles, and so on. How can I find these controls so I can apply the correct styles on them?

    if I add $(document).ready(function(){

       alert("something");

    });

    in the partial, it's never executed, is there another way to execute jquery in a partial so I can loop through the page and find the controls to apply style to them? 

    Wednesday, August 28, 2019 11:36 AM

Answers

  • User-2132746437 posted

    I got it working, with $(function() { //do something });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 28, 2019 1:04 PM

All replies

  • User475983607 posted

    Seems very odd that you are able to dynamically generate input elements but are unable to find the controls.  When you are generating the element you must know, at that time, what element is being rendered.  You should be able to assign a CSS class and/or a data attribute.  These can be used later to find the input.

    You can use CSS selectors to apply styles to every element type on the page without any extra code except the CSS selector.

    If you are unable to find the element now, how are you able to bind the parameters.  You need to know the input names.

    csharpcoder

    in the partial, it's never executed, is there another way to execute jquery in a partial so I can loop through the page and find the controls to apply style to them? 

    There is no such thing as a partial in the DOM.  A partial only exists on the server.   You should be able to just look at your HTML design to come up with an jQuery selector and loop over the inputs.

    This will loop over all the inputs on a page.

    $('input').each(function(index, value){
      //Your code
    });

    You can learn about jQuery selector on the jQuery reference page.

    https://api.jquery.com/category/selectors/

    There's also serialize().

    https://api.jquery.com/serialize/

    Wednesday, August 28, 2019 11:52 AM
  • User-2132746437 posted

    How can I execute the JS in the partial? I'm unable to get it executing. 

    Wednesday, August 28, 2019 12:09 PM
  • User-194957375 posted

    In Main Page : 

    $(document).ready(function(){

       function1();

    });

    in PartialView:

    <script>

    function1()

    {

    alert(111);

    }

    </script>

    this work?

    Wednesday, August 28, 2019 12:53 PM
  • User-2132746437 posted

    I got it working, with $(function() { //do something });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 28, 2019 1:04 PM