Bondsmith
Bondsmith

Reputation: 1578

Getting an attribute from the same html file I am working on

I am creating a plugin for a template. After publishing the template to the web, there is an attribute inside an inserted script that I need to get its value and use it in my plugin.

Is there a way to do it with JS/jQuery?

Here is the part of the page in which the attribute is located:

Platform.Utils.initWidget('#skyline', function (elem) { 
    new Website.tool.Constantin(elem, {
        event: 'click',
        transitionDuration: 93000
    }); 
});

I need to find a way to search the html and get the value for transitionDuration i.e 93000.

Additional comment:

This code is generated by the template and I have no control on changing how it is formed. I inspected the html, and the template places the code as a JS code ( "the code" ) somewhere in the body. So I assumed that there might be a way that the plugin that I am making could be able to read the html, find the attribute and use it to get the same transition duration that the html defines on its elements.

Upvotes: 0

Views: 51

Answers (2)

mplungjan
mplungjan

Reputation: 177684

After re-reading and seeing your comments, I assume your template inserts a script somewhere and you want to get at the transitionDuration with the plugin.

As in

    var scripts = document.getElementsByTagName("script"), duration=0;
    for (var i=0;i<scripts.length;i++) {
      var text = scripts[i].textContent;
      if (text.indexOf("transitionDuration") !=-1) { 
        duration = parseInt(text.split("transitionDuration:")[1],10);
        break;
      }
    }
    alert(duration);
    <script>
    Platform.Utils.initWidget('#skyline', function (elem) { 
      new Website.tool.Constantin(elem, {
        event: 'click',
        transitionDuration: 93000
      }); 
    });
    </script>

Upvotes: 1

Marcos P&#233;rez Gude
Marcos P&#233;rez Gude

Reputation: 22158

You can get CSS values in javascript and jQuery but in this case, is better if you store the value in a variable.

var transDuration = 93000;
Platform.Utils.initWidget('#skyline', function (elem) { 
   new Website.tool.Constantin(elem, {
      event: 'click',
      transitionDuration: transDuration
   }); 
});

alert(transDuration);

You can access to the transDuration variable when you need it.

If you need to read a CSS value, take a look at this:

Get a CSS value with JavaScript

Good luck

Upvotes: 0

Related Questions