简体   繁体   中英

Including JS to Contact Form 7 Wordpress

As you may think the problem is about including js to well-known wordpress plugin - Contact Form 7.

My form looks like this:

<label>[text* your-name] </label>
<label>[email* your-email]</label>
<label>[number number-189 min:500000000 max:900000000 id:numertel class:numertel1]</label> //this is telephone number
<label>[text* your-subject] </label>
<label>[textarea* your-message] </label>
[submit id:przycisk "Send"]

Users from stackoverflow helped me to repair my JS code which should enable submit button when telephone number length is 9 or 10.

And here it is the JS code:

document.getElementById("przycisk").disabled = true;
document.getElementById("numertel").addEventListener("keyup", function() {
  var numerlength = this.value.length;
  if (numerlength == 10 || numerlength == 9)
    document.getElementById("przycisk").disabled = false;
  else
    document.getElementById("przycisk").disabled = true;
});

And HTML generated by plugin which I saw from 'check element' option in a browser:

<input type="number" name="number-189" value="" class="wpcf7-form-control wpcf7-number wpcf7-validates-as-number numertel1" id="numertel" min="500000000" max="900000000" aria-invalid="false">
<input type="submit" value="Send" class="wpcf7-form-control wpcf7-submit" id="przycisk">

Code works perfect but I don't know how to include him to a plugin because something like this doesn't work:

<label>[text* your-name] </label>
<label>[email* your-email]</label>
<label>Telephone[number number-189 min:500000000 max:900000000 id:numertel class:numertel1]</label>
<label>[text* your-subject] </label>
<label>[textarea* your-message] </label>
[submit id:przycisk "Send"]
<script>
document.getElementById("przycisk").disabled = true;
document.getElementById("numertel").addEventListener("keyup", function() {
  var numerlength = this.value.length;
  if (numerlength == 10 || numerlength == 9)
    document.getElementById("przycisk").disabled = false;
  else
    document.getElementById("przycisk").disabled = true;
});
</script>

You can create additional shortcode for the given JS code and insert it after (not in,but after) contact form 7 tag.

So it would look like this

[contact-form-7 id="122" title="Contact Us"] [custom_js_for_cf7]

And here is the shortcode code for this, you can add this to your functions.php:

add_shortcode('custom_js_for_cf7','custom_js_for_cf7');
function custom_js_for_cf7($args){
return '
<script>
document.getElementById("przycisk").disabled = true;
document.getElementById("numertel").addEventListener("keyup", function() {
  var numerlength = this.value.length;
  if (numerlength == 10 || numerlength == 9)
    document.getElementById("przycisk").disabled = false;
  else
    document.getElementById("przycisk").disabled = true;
});
</script>
';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM