-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
74 lines (65 loc) · 2.68 KB
/
script.js
File metadata and controls
74 lines (65 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* vanilla js */
/*
const card = document.getElementById('card');
const heading = document.createElement('h1');
heading.innerText = 'Code Challenge #1';
const numVal = document.createElement('input');
numVal.setAttribute('id', 'num');
numVal.setAttribute('type', 'text');
numVal.setAttribute('placeholder', '0');
const paragraph = document.createElement('p');
paragraph.innerText =
'Create and style a centered card component like this with your own original design. Then add JavaScript logic to increment the hidden input number with each button click.';
const changeVal = document.createElement('button');
changeVal.setAttribute('id', 'changeVal');
changeVal.textContent = 'Change Value';
const footer = document.createElement('footer');
footer.innerHTML = 'ShaunPx1 get source code here: <a href="https://github.com/shaungt1/Code-Challenge-1" target="_blank">Git repo</a>';
card.append(heading, numVal, paragraph, changeVal, footer);
let incr = 0;
changeVal.addEventListener('click', () => {
numVal.value = incr++;
console.log(incr);
});
numVal.addEventListener('keydown', e => {
if (e.key === 'Enter') {
e.preventDefault();
incr = Number(numVal.value) || 0;
}
})
document.addEventListener('click', e => {
if (e.target !== numVal && e.target !== changeVal)
incr = Number(numVal.value);
}, true)
// -------------------------------------------------------------------------
/* jquery */
$(document).ready(() => {
const card = $('#card');
const heading = $('<h1></h1>').text('Code Challenge #1');
const numVal = $('<input>');
numVal.attr({
'id': 'num',
'type': 'text',
'placeholder': '0'
});
const paragraph = $('<p></p>').text('Create and style a centered card component like this with your own original design. Then add JavaScript logic to increment the hidden input number with each button click.');
const changeVal = $('<button></button>').attr('id', 'changeVal').text('Change Value');
const footer = $('<footer></footer>').html('ShaunPx1 get source code here: <a href="https://github.com/shaungt1/Code-Challenge-1" target="_blank">Git repo</a>');
card.append(heading, numVal, paragraph, changeVal, footer);
let incr = 0;
changeVal.click(() => {
numVal.val(incr++);
console.log(incr);
});
numVal.keydown(e => {
if (e.key === 'Enter') {
e.preventDefault();
incr = Number(numVal.val()) || 0;
numVal.blur();
}
})
$(document).click(e => {
if (e.target !== numVal[0] && e.target !== changeVal[0])
incr = Number(numVal.val());
})
});