Change given inside date attribute

Asked

Viewed 94 times

2

I have a page, which has a timer. That is, when the page opens, after 40 seconds, a Section of the site is loaded. This happens due to a third party service, I cannot handle this kind of information.

But I’d like to manipulate that in my browser.

For example, when loading the page, the attribute is set to 800.

data-delay-duration="800"

I would like to change that. I can’t do that directly on the site because as I commented, this is a third party service.

I installed the Tampermonkey for Google Chrome, I think there may be this possibility. But, I would like to know how to do that. It would be something with the attr jquery?

3 answers

4


If the browser does not support .dataset then you can use .setAttribute to exchange the value and .getAttribute to get him, so:

var teste = document.getElementById('teste');
teste.setAttribute("data-delay-duration", "100");

console.log("data-delay-duration:", teste.getAttribute("data-delay-duration"));
console.log("source:", document.body.innerHTML);
<span id="teste" data-delay-duration="800"></span>

Just for the record, although the kennel states that IE8, 9 e 10 has partial support to dataset and data- in https://caniuse.com/#feat=dataset, in fact they refer to the use of getAttribute same, that is to say .dataset not available, according to their message:

Partial support refers to being Able to use data-* Attributes and access them using getattribute.

So if you need one of these browsers you will have to use the.setAttribute.

If to apply for multiple elements, as a NodeList, can use any method for this (depending on the elements), as some examples:

  • document.getElementsByTagName
  • document.getElementsByName
  • document.querySelectorAll

I believe that the querySelectorAll is the simplest to use, so:

var els = document.querySelectorAll('.foo, .baz');

for (var i = 0, j = els.length; i < j; ++i) {
    els[i].setAttribute("data-delay-duration", "100");
}

console.log("source:", document.body.innerHTML);
<span class="foo" data-delay-duration="800"></span>
<span class="bar" data-delay-duration="800"></span>
<span class="baz" data-delay-duration="800"></span>
<span class="foo" data-delay-duration="800"></span>
<span class="foo" data-delay-duration="800"></span>
<span class="foo" data-delay-duration="800"></span>

Note that in this example only elements with class=foo and class=baz has the value exchanged

Note: to check if the DOM has already loaded you can run the script like this:

(function () { //Isola o escopo para evitar conflito com outras funções e vars

    function trigger() {
        var els = document.querySelectorAll('.foo, .baz');

        for (var i = 0, j = els.length; i < j; ++i) {
            els[i].setAttribute("data-delay-duration", "100");
        }
    }

    if (document.readyState !== "loading") {
        trigger();
    } else {
        document.addEventListener("DOMContentLoaded", trigger);
    }
})();
  • 1

    True William, have to have browser support, I did not put in my answer.

  • Thank you very much. Actually, this data-delay-Duration appears more than once on the site. Can’t use ID for this. In addition to being a third party service where I can’t handle it.

  • @Felipegoulart the ID is just example, I will formulate one with Nodelist, an instant

  • @Guilhermebirth an example of how many times this attribute appears on https://jsfiddle.net/ujhpawoe/1/

  • @Felipegoulart see the answer edition.

  • @Guilhermenascimento I understood his code. The funny thing is that he does not execute Tampermonkey

  • @Felipegoulart which error appears in the console?

  • @Guilhermenascimento does not show any error regarding the attribute. The strange thing is that Tampermonkey is activated. The data-delay-replace continues to be the same. The code looks like this https://jsfiddle.net/a0k32fmj/ The link would be this https://lp.luzdaserra.com.br/Initiesmonthly

  • @Felipegoulart see the answer edition.

  • @Guilhermenascimento unfortunately did not work. But, there must be some problem in Tampermonkey.

Show 5 more comments

0

If it’s with Vanilla you can do it like this:

var teste = document.getElementById('teste');
teste.dataset.delayDuration = 100;

console.log(teste.dataset.delayDuration);
<span id="teste" data-delay-duration="800"></span>

  • 2

    Something important to comment on: when doing delayduration = 100 you created another attribute. Javascript differentiates case from case, and in dataset, the attribute data-delay-duration will be dataset.delayDuration, with d in Duration capital.

  • Really Anderson, I was the one who wrote it wrong.

-3

Create this script in tampermonkey and exchange #test for the id you will use and the respective value on the line $('#teste').attr("data-delay-duration","400");.

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://stackoverflow.com/questions/10166478/how-to-change-the-value-of-a-custom-attribute
// @grant        none
// ==/UserScript==

// @require http://code.jquery.com/jquery-2.2.4.min.js

var $ = window.jQuery;

(function() {
    'use strict';
    console.log($('#teste').attr("data-delay-duration"));
    $('#teste').attr("data-delay-duration","400");
    console.log($('#teste').attr("data-delay-duration"));
    // Your code here...
})();
  • 2

    Could you add an explanation of what your code is doing? Yes, I was the one who denied it. I’m giving you the feedback, so you don’t run out of time.

  • Well... Using tampermonkey and jQuery, the answer is basically the line $('#teste').attr("data-delay-duration","400");, which arrow the attribute data-delay-duration with the value 400. The rest is to be able to run the example.

  • You need to add that to the answer. The idea of the site is that the question and the answer help, not only who is with the current problem, but other users who come here in the future. I suggest you click "edit" and add the details to your reply. I pledge myself in removing my negative in case something improves your response.

  • Well... I edited... but I do not agree with the downvotes, any programmer understands what was happening in the original post ;). Follow the dance.

  • 2

    Wrong, young man. It’s not just any programmer [at any level] who understands the code just by looking. It’s always good to have an explanation. People who are learning to program also access the site.

  • As promised, my downvote was removed.

  • Now note the answer with 0 votes.... Big difference, one with and the other without jQuery.... I was the target. hahaha

  • Particularly, I thought I’d use dataset was better than using attr, since the 'jQuery'também possui um recurso melhor que usarattr, que é a função date`. I commented on my vote, and I think this is the right one to know where you went wrong and improve. It’s a shame that other users don’t (fear of retaliation counts, as it has happened many times).

  • Cross-browser Consistency: The values of some Attributes are reported inconsistently Across browsers, and Even Across versions of a single browser. The .attr() method reduces such inconsistencies. But I won’t get into this discussion. You’re right.

  • Dear thxmxx what tampermonkey (add-on) has to do with the question? Your initial answer even though without explanation was much better than the current https://answall.com/revisions/322354/1 -- I recommend that you go back to the first one and just explain the code of the other one.

Show 5 more comments

Browser other questions tagged

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