Incrementing a class in jquery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
$(document).ready(function () { | |
let cartoonsCounter = 1; | |
let serialsCounter = 1; | |
const cartoon = `<div class="c-${cartoonsCounter}-wrapper rounded bg-gray-50"> | |
<input type="text" required class="c-${cartoonsCounter} w-80 text-center flex flex-col"> | |
<div class="serials mx-40"> | |
</div> | |
</div>`; | |
const serialInput = `<input type="text" required class="s-${serialsCounter} block border w-80 text-center my-2">`; | |
// start the page for an input with one cartoon unit | |
$(".cartoons-container").append(cartoon); | |
$("body").on("keyup", `.c-${cartoonsCounter}`, function (e) { | |
if (e.keyCode == 13) { | |
const c_value = $(`.c-${cartoonsCounter}`).val(); | |
console.log(c_value); | |
// if there is a serial | |
if (c_value.trim() !== "") { | |
// $(".serials").show(); | |
$(`.c-${cartoonsCounter}-wrapper .serials`).append(serialInput); | |
} | |
} | |
}); | |
$("body").on("keyup", `.s-${serialsCounter}`, function (e) { | |
if (e.keyCode == 13) { | |
const s_value = $(`.s-${serialsCounter}`).val(); | |
console.log(s_value); | |
// if there is a serial | |
if (s_value.trim() !== "") { | |
// $(".serials").show(); | |
serialsCounter++; | |
console.log(serialsCounter); | |
$(`.c-${cartoonsCounter}-wrapper .serials`).append( | |
`<input | |
type="text" | |
required | |
class="s-${serialsCounter} block border w-80 text-center my-2" | |
></input>` | |
); | |
} | |
} | |
}); | |
// //first serial input | |
// $(".s-1").keyup(function (e) { | |
// if (e.keyCode == 13) { | |
// const s_1_value = $(".s-1").val(); | |
// // if there is a serial | |
// if (s_1_value.trim() !== "") { | |
// $appened = $(".serials").append(serialInput); | |
// } | |
// } | |
// }); | |
}); | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment