How to automatically update the attribute value of any HTML element using Javascript?

Asked

Viewed 124 times

0

I need to keep the title of any element on the updated page. Whenever an element has the value of title being the character ?, I want to replace it with the value inside the tags of the same element.

The goal can be reached after calling the following function, using Jquery:

function preencherTitle() {

    var elementos = $('[title="?"]');

    elementos.each(function() {
        this.title = this.textContent;
    });
}

So I can call you when the page is ready, as follows:

$(preencherTitle);

The problem is that it does not work after DOM update by AJAX calls.
If I create a new element, by an AJAX call, that has [title="?"], the function is not called.

So I tried:

$(document).ajaxComplete(preencherTitle);

This also works, however, only when the AJAX call is marked as global.

I need the function to be triggered, too, when the AJAX call is not global.

I believe I may be treading the wrong path to solve this problem, even because I am not experienced in Javascript.

There is a way to achieve the objective of this function (even without necessarily using it) whenever necessary, and more performatively?

  • How are you making the AJAX call?

  • @Luizfelipe AJAX calls are performed by components of other frameworks, I have no control over it. At first, I don’t think I can consider how they are called, as there are several possible calls from a framework that I "don’t control".

  • You can’t fix it on the server?

No answers

Browser other questions tagged

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