1

What could be the best way /if there is any/ to add custom attribute to HTML element in Hubspot, more specifically, to the < form > element created by Hubspot?

<form id="form-id" myAtrribute="myValue"></form>

*Edit:

Static solution would be better, as it is going to be used in form analytics.

Thanks!

Silver Ringvee
  • 5,037
  • 5
  • 28
  • 46
  • 1
    you should use `data-*` attributes for custom attributes, eg. `data-myAttribute` (http://www.w3schools.com/tags/att_global_data.asp, http://stackoverflow.com/a/1735268/4339170) – CoderPi Dec 07 '15 at 12:09

1 Answers1

1

If you have access to the custom code section, and assuming you have jQuery, you can simply do:

<script>
   $("#form-id").attr("myAttribute", "myValue");
</script>

And if you do not have jQuery:

<script>
    document.getElementById("form-id").setAttribute("myAttribute", "myValue")
</script>
benjaminz
  • 3,118
  • 3
  • 35
  • 47
  • That is an option, but as that attribute is for form analytics (which does not play well with dynamycally loaded content) then I am looking for more "static" solution for it. Although it looks like there's no built-in option in Hubspot. Thanks a lot anyways! – Silver Ringvee Dec 07 '15 at 12:57
  • @SilverRingvee not sure what do you mean by "static", do you mean directly edit the HTML? I've used Hubspot for a little bit and I'm not sure if you can directly edit the form's HTML from the CMS. – benjaminz Dec 07 '15 at 13:00
  • By static I mean it would be loaded as form loads (not after form has loaded). Well, I know I can change templates for example, but the form itself is then loaded into the template, dynamically. I haven't seen any option to add even a custom class to my form in Hubspot editor. – Silver Ringvee Dec 07 '15 at 13:35
  • @SilverRingvee I see your point, right my approach wouldn't work unless the form is loaded. Why does it have to be loaded with the form? If you are using scripts for analytics, then as long as this JS injection finishes before your analytics script starts running, it should be fine. – benjaminz Dec 07 '15 at 13:38
  • if I get the timing right then yes, it should work. The thing is, analytics platform indexes forms by their ID value, or a specific custom attribute, if that is present. I also asked Hubspot for their solution for the issue, let's see what they'll answer! – Silver Ringvee Dec 07 '15 at 14:05
  • @SilverRingvee if your analytics script is loaded with a ` – benjaminz Dec 07 '15 at 14:11