admin管理员组文章数量:1335371
I have added 2 buttons in my page and I have added the default button click code using javascript but it is not working.
This is my HTML code:
<button class="tablink" onclick="openPage('inside', this, '#0077c0')" id="defaultOpen">Inside</button>
<button class="tablink" style="left:40px;" onclick="openPage('outside', this, '#0077c0')" >Outside</button>
This is JS code added in functions.php
:
function wp_head_custom_mm(){
?>
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>
<?php
}
add_action('wp_head', 'wp_head_custom_mm');
But it is not working, my default button is not clicked.
Error:
Uncaught TypeError: Cannot read property 'click' of null
I want to button with id defaultOpen
should be clicked when the website opens.
Any help is much appreciated.
I have added 2 buttons in my page and I have added the default button click code using javascript but it is not working.
This is my HTML code:
<button class="tablink" onclick="openPage('inside', this, '#0077c0')" id="defaultOpen">Inside</button>
<button class="tablink" style="left:40px;" onclick="openPage('outside', this, '#0077c0')" >Outside</button>
This is JS code added in functions.php
:
function wp_head_custom_mm(){
?>
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>
<?php
}
add_action('wp_head', 'wp_head_custom_mm');
But it is not working, my default button is not clicked.
Error:
Uncaught TypeError: Cannot read property 'click' of null
I want to button with id defaultOpen
should be clicked when the website opens.
Any help is much appreciated.
Share Improve this question asked Jul 17, 2020 at 20:54 Rahul PamnaniRahul Pamnani 1051 silver badge13 bronze badges1 Answer
Reset to default 2Anytime you want to use JavaScript in WordPress, you should enqueue it.
Save the JS as a file, ie clickbutton.js
inside your theme folder:
document.getElementById("defaultOpen").click();
Then in functions.php
, enqueue that file:
add_action('wp_enqueue_scripts', 'wpse_371294_enqueue_js');
function wpse_371294_enqueue_js() {
wp_enqueue_script('click-button', get_template_directory_uri() . '/clickbutton.js', array(''), "1.0", true);
}
(If you only need the file to run on one page, enqueue it conditionally so it doesn't get loaded elsewhere.)
However, note that you haven't set any trigger for this to fire. For this type of JS to work, you will likely need to use a trigger such as
jQuery( document ).ready(function() {
document.getElementById("defaultOpen").click();
});
so that the code runs when the page loads fully. If you go that route, you'll also need to make sure that you enqueue the script with jQuery as a dependency:
add_action('wp_enqueue_scripts', 'wpse_371294_enqueue_js');
function wpse_371294_enqueue_js() {
wp_enqueue_script('click-button', get_template_directory_uri() . '/clickbutton.js', array('jquery'), "1.0", true);
}
And on a final note, it's likely you could achieve this same effect with CSS. Have whatever the button does load by default, use CSS to show it rather than hide it, and then if someone clicks to close use CSS to hide. It depends on exactly what you're doing, but there's often a way to avoid running JS for an initial page state.
本文标签: htmlMy Button Default Click Is Not Working In WordPress Using Javascript Code
版权声明:本文标题:html - My Button Default Click Is Not Working In WordPress Using Javascript Code 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742253248a2441155.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论