jquery cookie issue - does not open modal window - jquery-cookie

I have small issue with jquery-cookie plugin, as you can see in functions.php , first I register script, then enqueue jquery_cookie_script, but does not work properly... I want to archive when user open website today, and scroll.Top() > 50% of window height, modal window will be opened. When close modal window by click on the X button, will not be able to open it again after refresh the whole page, only for 7 days.
You can see my code below, I need some help how to fix that issue, because does not work at all.
jQuery(document).ready(function($) {
$(window).scroll(function(event) {
var y = $(this).scrollTop();
var half_height = $(window).height() / 2;
if (y > half_height) {
if ($.cookie('modal_shown') == null) {
$.cookie('modal_shown', 'yes', {
expires: 7,
path: '/'
});
$('#modal').reveal();
}
}
});
});
in functions.php
function enqueue_custom_js(){
wp_enqueue_script('custom', get_stylesheet_directory_uri() . '/js/custom.js',array(),time());
wp_register_script( 'jquery_cookie_script', 'http://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js',array(),time());
wp_enqueue_script( 'jquery_cookie_script');
}
add_action('wp_enqueue_scripts','enqueue_custom_js');
HTML
<!-- Modal -->
<div class="modal fade" id="modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<div class="modal-body text-center">
<span class="title_subtitle white">Hey babe!<br>Let's link up!</span><br>
<span class="get_off">Get 15% OFF</span><br>
<span class="first_order white">your first order</span><br>
<span class="arrivals white">new arrivals, contests and promos to your inbox</span><br>
<input type="email" name="email" placeholder="Email" required>
<button class="btn subscribe-btn" value="Submit">Join Now</button>
</div>
<div class="modal-footer text-center">
<a data-dismiss="modal" title="No thanks, I don't need a Promo Code">No thanks, I don't need a Promo Code</a>
</div>
</div>
</div>
</div>

I found a solution that works properly
jQuery( document ).ready( function( $ ) {
$(document).on('scroll', function() {
var y = $(this).scrollTop();
var half_height = $(window).height()/2;
if (y > half_height) {
$(document).off('scroll');
if ($.cookie('pop') == null) {
$('#modal').modal('show');
$.cookie('pop', '7');
}
}
});
});

Related

bootstrap modal ajax send two time on submit

When I open a modal, then click the submit (to send ajax post request for instance) and if I close the modal, reopen it again and re-submit it send 2 times the request (event with e.preventDefault() )
I don't understand what is wrong
<div class="modal fade bd-example-modal-lg" id="modalA" tabindex="-1" role="dialog"
aria-labelledby="modalChangeTypeLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body d-flex justify-content-center">
<select name="nameA" id="idA" class="form-control" required>
</select>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" id="btnChangeStatusA" class="btn btn-primary">ChangeA</button>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function () {
$("#btnA").click(function () {
$("#modalA").modal();
$("#btnChangeStatusA").click('input', function (f) {
f.preventDefault()
alert('ok')
})
})
})
</script>
https://jsfiddle.net/e0tah93b/
it seems to be stacking the calls when you click on #btnA, try doing this instead:
$(document).ready(function () {
$("#btnA").click(function () {
$("#modalA").modal();
})
$("#btnChangeStatusA").click('input', function (f) {
f.preventDefault()
alert('ok')
})
})

How can I open a modal in VueJS using bootstrap?

I am trying to open a modal in my Vue Template using boostrap. but whenever I try using jquery on it, modal is not appearing.
Here is my code:
<template>
<div id="app_religion">
<!-- Button trigger modal -->
<!-- <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">Open</button> -->
<button type="button" class="btn btn-primary" #click="showModal()">
Open
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
test
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
methods: {
showModal() {
console.log("test")
$('#exampleModal').modal('show');
},
},
}
</script>
I am really new in using Vue, can anyone guide me on how to solve my problem. I am really lost on trying to figure it out.
This can be easily implemented through plain js
Start by assigning a unique id to your modal
<div class="modal fade" id="uniqueId" tabindex="-1" role="dialog" aria-labelledby="uniqueIdlLabel" aria-hidden="true">
import Modal from bootstrap
import { Modal } from "bootstrap";
Declare a variable that will serve as your proxy to your modal, declared as uniqueModal below, Assign the variable the DOM element retrieved by uniqueId, use the inbuilt functions provided by bootstrap to manipulate the modal. Documentation
export default {
name:"randomName",
data(){
return {uniqueModal:null}
},
methods:
{
showUniqueModal() {
this.uniqueModal = new Modal(document.getElementById("uniqueId"),{ keyboard: false });
this.uniqueModal.show();
},
closeUniqueModal() {
this.uniqueModal.hide();
},
},
}
Why trying to do it with jQuery when you can make it with vue ?
use v-if or v-show on your modal (check the difference between the two and take the one who fits better the result you want)
https://v2.vuejs.org/v2/guide/conditional.html
Didn't tested it, i wrote it on the fly, but something like this should work.
<template>
<div>
<button type="button" class="btn btn-primary" #click="showModal">
Open
</button>
<div class="modal" v-if="modalOpen">
<p>Hello World</p>
</div>
</div>
</template>
<script>
export default {
data() {
return {
modalOpen: false
}
},
methods: {
showModal() {
this.modalOpen = true;
}
}
}
</script>

Bootstrap 4 destroy a modal when it is closed

I'm looking for a solution on my problem. I'm using bootstrap4 on laravel project. I have a modal that has a button which close it self and open a new modal. This is the code:
$("#newAddressBtn").on('click', function () {
$('#modalAddressees').modal('hide')
$('#modalAddressees').on('hidden.bs.modal', function () {
// Load up a new modal...
$('#modalNewAddress').modal('show');
})
});
Inside this new modal (modalNewAddress) I have a button that dismiss it. But when I dismiss it, I would to destroy it self because if I need to re-open it again, I would to start with empty data inside (such us the first show).
I wrote this code but nothing happens:
[....]
.on('core.form.valid', function () {
$.ajax({
url: "{{ route("addresses.store") }}",
headers: {'X-CSRF-Token': $('meta[name=_token]').attr('content')},
dataType: "JSON",
data: $("#addressForm").serialize(),
type: "POST",
success: function (data) {
destroyModal("#modalNewAddress");
},
error: function (data) {
console.log('error ' + data);
}
});
});
[....]
function destroyModal(modal){
$(modal).modal('dispose');
}
This is the code of the modal:
<div class="modal fade" tabindex="-1" role="dialog" id="modalNewAddress">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">New Address</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form method="POST" id="addressForm" action="{{ route("addresses.store") }}" class="needs-validation" novalidate="">
{{ csrf_field() }}
<div class="form-group row">
<label for="name" class="col-sm-3 col-form-label">Name</label>
<div class="col-sm-9">
<input type="text" class="form-control" name="name" id="name" value="" placeholder="Name" required="">
</div>
</div>
<span class="float-right">
<button type="submit" id="save" class="btn btn-primary">Save</button>
</span>
<input type="hidden" name="ajax">
</form>
</div>
<div class="modal-footer bg-whitesmoke br">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
How can I solve the problem? Could you please help me?
You're not supposed to destroy the modal but recycle it.
The usage pattern would be as follows:
the modal does not contain data, but data fields you can reach with jQuery, such as:
<div id="adress-nome"></div>
<!-- more fields ... -->
before you open the modal, you populate it with a function, such as:
function updateModal(data)
{
$("#address-nome").val(data.nome);
// more data ...
}
function openModal(data)
{
updateModal(data);
$("#address-modal").modal("show");
}
when you're done with the modal, just hide it
when you re-open it, it will contain the new data

bootstrap 4 modal not showing after button is clicked with VueJs

I just learned vuejs with bootstrap 4 and I tried to display modal provided that when the create button was not clicked then the HTML modal tag is not displayed in the inspect element, and after the create button click bootstrap modal displayed. When the button create first time click HTML modal tag display on inspect element but bootstrap modal cannot be displayed on the browser page and the second time the bootstrap modal can be displayed. Here is the source code that I made with Laravel, can you help me on this issue, thank you.
User.vue
HTML
<template>
<div class="container">
<div class="row">
<div class="col-sm">
<div class="card-deck mb-3">
<div class="card mb-4 box-shadow">
<div class="card-header bg-success text-white">
<h4 class="my-0 font-weight-bold">
<!-- Button create user -->
<button #click="initAddUser()" class="btn btn-danger btn-sm"><i class="fa fa-plus"></i> Create New User</button>
<span class="float-right">User</span>
</h4>
</div>
<div class="card-body">
<!-- Bootstrap modal -->
<div v-if="showModal" class="modal fade" id="add_user_model" tabindex="-1" role="dialog" aria-labelledby="add_user_model_label" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header bg-info text-white">
<h5 class="modal-title" id="add_user_model_label"><i class="fa fa-plus"></i> Add New User</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<div class="modal-body">
<!-- Message errors create user -->
<div class="alert alert-danger" v-if="errors.length > 0">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<h5>{{ messages }}</h5>
<ul>
<li v-for="error in errors">{{ error }}</li>
</ul>
</div>
<div class="form-group">
<label class="font-weight-bold" for="name">Name :</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Name" v-model="user.name">
</div>
<div class="form-group">
<label class="font-weight-bold" for="name">Email :</label>
<input type="email" class="form-control" id="email" name="email" placeholder="E-mail" v-model="user.email">
</div>
<div class="form-group">
<label class="font-weight-bold" for="password">Password:</label>
<input type="password" class="form-control" id="password" name="password" placeholder="Password" v-model="user.password">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-sm btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-sm btn-primary" #click="createUser()">Submit</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</template>
Js
<script>
export default {
data() {
return {
user: {
name: '',
email: '',
password: ''
},
errors: [],
users: [],
showModal: false,
}
},
mounted() {
this.readUsers();
},
methods: {
initAddUser() {
this.errors = [];
$("#add_user_model").modal("show");
this.showModal = true;
},
reset() {
this.user.name = '';
this.user.email = '';
this.user.password = '';
},
readUsers() {
axios.get('/user/show')
.then(response => {
this.users = response.data.users;
});
},
createUser() {
axios.post('/user', {
name: this.user.name,
email: this.user.email,
password: this.user.password,
})
.then(response => {
this.reset();
this.users.push(response.data.user);
this.readUsers();
$("#add_user_model").modal("hide");
})
.catch(error => {
this.errors = [];
this.messages = error.response.data.message;
if (error.response.data.errors.name) {
this.errors.push(error.response.data.errors.name[0]);
}
if (error.response.data.errors.email) {
this.errors.push(error.response.data.errors.email[0]);
}
if (error.response.data.errors.password) {
this.errors.push(error.response.data.errors.password[0]);
}
});
}
}
}
</script>
Result images inspect element
This image button first time click https://ibb.co/jKENbU
This image button second time click https://ibb.co/nRy4qp

Components. Send request with file via vue.http.post()

I have vuejs2 component on page (single file component). It's a simple bootstrap modal window with one file input. I only need to upload one file (no mutlifile upload or etc.)
What is in file:
<template>
<div class="modal fade" id="upload-file-modal" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Upload file</h4>
</div>
<form id="app-file-upload-form" name="appFileUploadForm" #submit.prevent="uploadAppFile" novalidate enctype="multipart/form-data">
<div class="modal-body">
<div class="form-group">
<label for="app-file-fileinput">File</label>
<input type="file" name="file" id="app-file-fileinput" class="form-control" v-el="file" #change="attachFile">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Upload</button>
</div>
</form>
</div>
</div>
</div>
<script>
export default {
data() {
return {
file: ''
}
},
methods: {
attachFile(e) {
var files = e.target.files || e.dataTransfer.files;
if (!files.length)
return;
this.file = files[0];
},
uploadAppFile() {
console.log(this.file);
Vue.http.post('/api/v1/apps/' + this.appId + '/files', { file: this.file}).then((response) => {
console.log(response);
}, (response) => {
console.log(response);
});
}
}
}
So i have laravel controller for handle it.
public function upload(Request $request)
{
$attachedFile = $request->file;
return response()->json($request->all());
}
I put return response->json() for kinda debug this. When i attach file, file attaching to component(model?) field and not empty. But when vue.http.post happening the file comes to empty object.
First is console.log(file)
Second is server response.
I also tried with FormData()... it didn't work.
ps: i cut version field from code.
After few more times with formdata i got file upload working. Just put variable inside method.
uploadAppFile() {
let data = new FormData();
data.append('file', this.file);
Vue.http.post('/api/v1/apps/' + this.appId + '/files', data).then((response) => {});
}

Resources