How to call partial function after added via append

Asked

Viewed 495 times

2

Hi, I have a partial view, that I surrender with the following code:

        $.ajax({
            url: URL,
            success: function (data) {
                $("#DIV").append(data);
            }
        });

And within that page, I call a function

<script> 
   Funcao(selectorId);
</script>

I need this function to be called inside the partial view, after being loaded with append

Because the parameter (selectorId) of the function is received only, so every time I call to render in $("#DIV").append(data); the selectorId parameter will be different...

How can I do that?


My element is mounted like this in the ASP.NET code:

<input id="Contatos[@Guid.NewGuid().ToString()].Nome" name="Contatos[@Guid.NewGuid().ToString()].Nome" type="text" value="0">

Example of resulting ID:

Contatos[8902dbfd-e856-48c6-8f17-d0548b2dea62].Nome
  • If I understand correctly, what you have should work. There is some error in the console?

  • not @bfavaretto , however, it seems to me that the element when I call my function, has not yet been rendered, as it does not find some date attributes...

  • When you say "within that page" refers to partial? The script is at the end of partial?

  • Yes, it should be called after the append, but the script is inside the partial, is that I Gero the attribute "ID" that is passed to the function, internally in Partial, so each time I make a call of this partial the attribute Id will be a different...

  • You cannot return this value in the ajax call and run the function there with this ID?

  • @bfavaretto seeing his answers, saw that works or worked with c#, well, the idea, is that I feed my elements the elements like this: <input id="Contatos[@Guid.NewGuid().ToString()].Nome" name="Contatos[@Guid.NewGuid().ToString()].Nome" type="text" value="0"> Then I call the function by passing the parameter Id, and there in the function use $("#" + parameter") to pick up the element

  • @Andréribeiro can not say, since I Gero the attribute Id there in the partial view, as I showed in the comment above

  • @Rod I think your JS code is ok. The problem may be that your selector contains a ., that jQ will interpret as class selector. Try to escape the point with \. in office.

  • @bfavaretto can be...my Id is being generated like this: Contatos[8902dbfd-e856-48c6-8f17-d0548b2dea62].Nome But as it goes on: Contatos[8902dbfd-e856-48c6-8f17-d0548b2dea62]\\.Nome I was also unsuccessful

  • Brackets can also be problem, have another sense in selectors.

  • @bfavaretto exactly :D worked...if you want to provide an answer, I add too much detail to the question

  • I will try to post later if no one does this before :) I take this opportunity to record the link of a related question: http://answall.com/questions/211/como-usar-um-nome-com-colchete-par%C3%Aanteses-retos-num-selector-de-jquery

Show 7 more comments

2 answers

2


This type of ID has several characters that are not interpreted literally when used in a selector. For example, a selector like this:

#Contatos[8902dbfd-e856-48c6-8f17-d0548b2dea62].Nome

Find an element whose ID is Contatos, which has an attribute called 8902dbfd-e856-48c6-8f17-d0548b2dea62 and who has the class Nome. For example:

<div id="Contatos" class="Nome" 8902dbfd-e856-48c6-8f17-d0548b2dea62></div>

But that’s not what you want... So it is necessary to escape the characters that are causing the confusion, [, ] and .. jQuery needs to get this:

#Contatos\[8902dbfd-e856-48c6-8f17-d0548b2dea62\]\.Nome

So the string needs to be mounted like this:

"#Contatos\\[8902dbfd-e856-48c6-8f17-d0548b2dea62\\]\\.Nome"

Reference: How to use a bracketed name (square brackets) in a jQuery selector?

1

Create the function in partial without calling it and call after the append.

        $.ajax({
        url: URL,
        success: function (data) {
            $("#DIV").append(data);
            funcaoDentroDaPartial();
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.