Skip to content
Advertisement

Edit DOM element in var before append it

I have a function that appends elements to the page who calls it using a template, like this:

JavaScript

I have a function parameter that says which of the appended element sections is visible and which is not. So what I want to do is access to an element in the test variable like I do with jQuery and disable it.

Is there any way to do this?

PD: I know that I can do this after the append, but I would like to do it before.

Advertisement

Answer

After the $.tmpl call, the resulting jQuery object should be able to support all of the usual jQuery methods you’d need to disable elements, even if they haven’t been added to the DOM yet. For example:

JavaScript

Use the .find() method to focus on a specific set of elements by passing the usual selector string. The OP mentions “disabling” elements, so I think the .prop('disabled', true) method is what you’re looking for. Finally, the .end() method makes sure that you append the entire batch of HTML to the document, not just the results of the .find().

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement