CMS Development

fayettec
Member

Onlcick is stripped from my code

SOLVE

When I add this to my html, 

<button onclick='toggleInsert1()' id="insertToggleButton">See Analytics</button>

I get this after i save it

<button id="insertToggleButton">See Analytics</button>

0 Upvotes
1 Accepted solution
tjoyce
Solution
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

Onlcick is stripped from my code

SOLVE

@fayettec - If you're adding it in a wysiwyg, hubspot does some things to sanitize the html and maintain scopes of javascript (mostly seen in embed codes but, could be happening in this scenario as well). It's best to use the following code:

<script>
$(function(){
  $('#insertToggleButton').on('click', function(){
    toggleInsert1();
  });
});
</script>

If this answer helped, please, mark as solved 😄


tim@belch.io | forms.belch.io | Design your own Beautiful HubSpot Forms; No coding necessary.

 

Drop by and say Hi to me on slack.

View solution in original post

9 Replies 9
fayettec
Member

Onlcick is stripped from my code

SOLVE

Thanks, But that didn't work either. Causing an exception.

0 Upvotes
tjoyce
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

Onlcick is stripped from my code

SOLVE

@fayettec - It would help to see the full JS error or a preview link to the page causing the issues. The error is probably unrelated to your original issue.

0 Upvotes
fayettec
Member

Onlcick is stripped from my code

SOLVE

$(function{ x  
$('#insertToggleButton').on('click', function(){
toggleInsert1();
});
});

 

Uncaught SyntaxError: Unexpected token {

the x is where chrome indicated an error

0 Upvotes
tjoyce
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

Onlcick is stripped from my code

SOLVE

@fayettec - My apologies, I forgot a syntax in the code (it was untested). I updated my original answer if you can copy the code again.

0 Upvotes
Jsum
Key Advisor

Onlcick is stripped from my code

SOLVE

@fayettec,

 

It does sound like you are trying to put the code in the the source code for a richtext area. I know that the rich text editor has rules for it's markup and will manipulate mark up to match those rules. <a> tags with out the "href" attribute are removed as well. 

 

You can use jquery to insert the onlclick on page load like @tjoyce said, you can write an onclick function that targets a unique class on the button:

<button class="the_button">click</button>

$('.the_button').on('click touch', function(){

});

or you can find a way to hard code the button, such as by using a custom module.

 

Need help? Hire Us Here

fayettec
Member

Onlcick is stripped from my code

SOLVE

Thanks @Jsum

0 Upvotes
tjoyce
Solution
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

Onlcick is stripped from my code

SOLVE

@fayettec - If you're adding it in a wysiwyg, hubspot does some things to sanitize the html and maintain scopes of javascript (mostly seen in embed codes but, could be happening in this scenario as well). It's best to use the following code:

<script>
$(function(){
  $('#insertToggleButton').on('click', function(){
    toggleInsert1();
  });
});
</script>

If this answer helped, please, mark as solved 😄


tim@belch.io | forms.belch.io | Design your own Beautiful HubSpot Forms; No coding necessary.

 

Drop by and say Hi to me on slack.

fayettec
Member

Onlcick is stripped from my code

SOLVE

@tjoyceThanks for your Help.

0 Upvotes
fayettec
Member

Onlcick is stripped from my code

SOLVE

I also found that comments inside my JS caused HubSpot to make the entire function a comment 😞

0 Upvotes