Created
July 6, 2021 04:02
-
-
Save xiaocuixt/01712f4c2d8c3f758225b7c4b7585b64 to your computer and use it in GitHub Desktop.
format json in textarea
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
// preview with hilight.js and pretty json format | |
$('[name="json_data"]').on('input change', function () { | |
let previewWrapper = $(this).siblings('.preview-wrapper'); | |
const $code = previewWrapper.find('pre > code'); | |
if ($(this).val()) { | |
try { | |
var val = JSON.parse($(this).val()) | |
} catch (e) { | |
$(this).addClass("warning-bordered"); | |
return false; | |
} | |
$(this).removeClass("warning-bordered"); | |
previewWrapper.removeClass("hide"); | |
$code.text(JSON.stringify(val, undefined, 2)) | |
hljs.highlightBlock($code[0]); | |
} else { | |
previewWrapper.addClass("hide"); | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment