Here you can see my website and error
when I click on button my website is not redirecting to that link only website start reloading
function send() {
var name = document.getElementById('name').value;
var email = document.getElementById('mail').value;
var message = document.getElementById('descriptions').value;
if (name && email && message === '') {
window.navigator.vibrate('200');
alert('Enter somethingđ');
} else {
window.location.href = 'https://wa.me/919811192234/?text=' + 'Hy' +
' ' + 'I' + ' ' + 'Am' + ' ' + name + ' ' + email.value + ' ' + message + ' '
}
console.log(name && email && message)
}
<div class="contactus" id="contact">
<p align="center">CONTACT US</p>
<hr width="50%" size="5px" color="aqua">
<form action="" id="demo-form">
<input type="text" placeholder="Enter Name*" id="name">
<input type="email" placeholder="Enter Email*" id="mail">
<textarea placeholder="Message For Us*" id="descriptions" cols="30" rows="10"></textarea>
<button type="submit" class="submit1" onclick="send()">Submit</button>
</form>
</div>
I want that when user click on that button he will redirect to whatsapp
4
Answers
It seems like your button is submitting the form, what you can do is add a
onSubmit
handler on your form, which looks something like:and then in your javascript:
You can use window.open(URL, "_blank") so that it will open that in a new tab and this will work.
The button is (by default)
type="submit"
. Clicking the button will submit its form owner by default.You are adding a
click
listener but don’t prevent the click’s default action of submitting its form owner.This means your redirection is being overriden by the submission’s action.
To prevent this you should prevent the event’s default action by calling
event.preventDefault()
:click
event’s default action prevents the followingsubmit
event from dispatching.submit
event’s default action prevents the form’s default action (i.e. ‘redirection’).Since you handle submission logic, you should use a
submit
listener. Using asubmit
listener on the form over aclick
listener on the button comes with some benefits:Changing
<button>
‘stype
attribute fromsubmit
tobutton
will give you desired behavior, because it won’t firesubmit
event, this way default behavior(page reload) won’t happen.