jQuery code to detect whether form fields have the default value when a checkbox is unchecked
$begingroup$
How could I rewrite this jquery code to compact it in a more elegant form:
$(document).ready(function(){
$('input[name="table_exist"]').click(function(){
if($('input[name="table_name"]').val()=='users' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="table_name"]').val('');
}
else if($('input[name="table_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="table_name"]').val('users');
}
if($('input[name="user_id"]').val()=='user_id' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_id"]').val('');
}
else if($('input[name="user_id"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_id"]').val('user_id');
}
if($('input[name="user_name"]').val()=='user_name' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_name"]').val('');
}
else if($('input[name="user_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_name"]').val('user_name');
}
if($('input[name="user_email"]').val()=='user_email' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_email"]').val('');
}
else if($('input[name="user_email"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_email"]').val('user_email');
}
if($('input[name="user_pass"]').val()=='user_pass' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_pass"]').val('');
}
else if($('input[name="user_pass"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_pass"]').val('user_pass');
}
if($('input[name="joining_date"]').val()=='joining_date' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="joining_date"]').val('');
}
else if($('input[name="joining_date"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="joining_date"]').val('joining_date');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="POST">
<h2>DB Table</h2>
<p>
Activate the slider if you already have your own database table created!
</p>
<div class="inputCheckBox">
<input type="checkbox" name="table_exist" />
</div>
<p>
Edit here only if you have your own predifined table header names!
</p>
<div class="inputBox">
<input type="text" name="table_name" value="users" required />
<label>Table Name</label>
</div>
<div class="inputBox">
<input type="text" name="user_id" value="user_id" required />
<label>User ID</label>
</div>
<div class="inputBox">
<input type="text" name="user_name" value="user_name" required />
<label>User Name</label>
</div>
<div class="inputBox">
<input type="text" name="user_email" value="user_email" required />
<label>User E-mail</label>
</div>
<div class="inputBox">
<input type="text" name="user_pass" value="user_pass" required />
<label>User Password</label>
</div>
<div class="inputBox">
<input type="text" name="joining_date" value="joining_date" required />
<label>User Added Date</label>
</div>
<button type="submit" name="btn-install">
Install
</button>
</form>
The whole process is to identify if the fields have the default values when a checkbox is unchecked. If the state is changed to checked then empty all fields to input custom information/values. If unchecked again it returns the initial values.
javascript jquery form
$endgroup$
add a comment |
$begingroup$
How could I rewrite this jquery code to compact it in a more elegant form:
$(document).ready(function(){
$('input[name="table_exist"]').click(function(){
if($('input[name="table_name"]').val()=='users' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="table_name"]').val('');
}
else if($('input[name="table_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="table_name"]').val('users');
}
if($('input[name="user_id"]').val()=='user_id' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_id"]').val('');
}
else if($('input[name="user_id"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_id"]').val('user_id');
}
if($('input[name="user_name"]').val()=='user_name' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_name"]').val('');
}
else if($('input[name="user_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_name"]').val('user_name');
}
if($('input[name="user_email"]').val()=='user_email' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_email"]').val('');
}
else if($('input[name="user_email"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_email"]').val('user_email');
}
if($('input[name="user_pass"]').val()=='user_pass' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_pass"]').val('');
}
else if($('input[name="user_pass"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_pass"]').val('user_pass');
}
if($('input[name="joining_date"]').val()=='joining_date' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="joining_date"]').val('');
}
else if($('input[name="joining_date"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="joining_date"]').val('joining_date');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="POST">
<h2>DB Table</h2>
<p>
Activate the slider if you already have your own database table created!
</p>
<div class="inputCheckBox">
<input type="checkbox" name="table_exist" />
</div>
<p>
Edit here only if you have your own predifined table header names!
</p>
<div class="inputBox">
<input type="text" name="table_name" value="users" required />
<label>Table Name</label>
</div>
<div class="inputBox">
<input type="text" name="user_id" value="user_id" required />
<label>User ID</label>
</div>
<div class="inputBox">
<input type="text" name="user_name" value="user_name" required />
<label>User Name</label>
</div>
<div class="inputBox">
<input type="text" name="user_email" value="user_email" required />
<label>User E-mail</label>
</div>
<div class="inputBox">
<input type="text" name="user_pass" value="user_pass" required />
<label>User Password</label>
</div>
<div class="inputBox">
<input type="text" name="joining_date" value="joining_date" required />
<label>User Added Date</label>
</div>
<button type="submit" name="btn-install">
Install
</button>
</form>
The whole process is to identify if the fields have the default values when a checkbox is unchecked. If the state is changed to checked then empty all fields to input custom information/values. If unchecked again it returns the initial values.
javascript jquery form
$endgroup$
add a comment |
$begingroup$
How could I rewrite this jquery code to compact it in a more elegant form:
$(document).ready(function(){
$('input[name="table_exist"]').click(function(){
if($('input[name="table_name"]').val()=='users' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="table_name"]').val('');
}
else if($('input[name="table_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="table_name"]').val('users');
}
if($('input[name="user_id"]').val()=='user_id' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_id"]').val('');
}
else if($('input[name="user_id"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_id"]').val('user_id');
}
if($('input[name="user_name"]').val()=='user_name' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_name"]').val('');
}
else if($('input[name="user_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_name"]').val('user_name');
}
if($('input[name="user_email"]').val()=='user_email' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_email"]').val('');
}
else if($('input[name="user_email"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_email"]').val('user_email');
}
if($('input[name="user_pass"]').val()=='user_pass' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_pass"]').val('');
}
else if($('input[name="user_pass"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_pass"]').val('user_pass');
}
if($('input[name="joining_date"]').val()=='joining_date' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="joining_date"]').val('');
}
else if($('input[name="joining_date"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="joining_date"]').val('joining_date');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="POST">
<h2>DB Table</h2>
<p>
Activate the slider if you already have your own database table created!
</p>
<div class="inputCheckBox">
<input type="checkbox" name="table_exist" />
</div>
<p>
Edit here only if you have your own predifined table header names!
</p>
<div class="inputBox">
<input type="text" name="table_name" value="users" required />
<label>Table Name</label>
</div>
<div class="inputBox">
<input type="text" name="user_id" value="user_id" required />
<label>User ID</label>
</div>
<div class="inputBox">
<input type="text" name="user_name" value="user_name" required />
<label>User Name</label>
</div>
<div class="inputBox">
<input type="text" name="user_email" value="user_email" required />
<label>User E-mail</label>
</div>
<div class="inputBox">
<input type="text" name="user_pass" value="user_pass" required />
<label>User Password</label>
</div>
<div class="inputBox">
<input type="text" name="joining_date" value="joining_date" required />
<label>User Added Date</label>
</div>
<button type="submit" name="btn-install">
Install
</button>
</form>
The whole process is to identify if the fields have the default values when a checkbox is unchecked. If the state is changed to checked then empty all fields to input custom information/values. If unchecked again it returns the initial values.
javascript jquery form
$endgroup$
How could I rewrite this jquery code to compact it in a more elegant form:
$(document).ready(function(){
$('input[name="table_exist"]').click(function(){
if($('input[name="table_name"]').val()=='users' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="table_name"]').val('');
}
else if($('input[name="table_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="table_name"]').val('users');
}
if($('input[name="user_id"]').val()=='user_id' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_id"]').val('');
}
else if($('input[name="user_id"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_id"]').val('user_id');
}
if($('input[name="user_name"]').val()=='user_name' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_name"]').val('');
}
else if($('input[name="user_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_name"]').val('user_name');
}
if($('input[name="user_email"]').val()=='user_email' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_email"]').val('');
}
else if($('input[name="user_email"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_email"]').val('user_email');
}
if($('input[name="user_pass"]').val()=='user_pass' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_pass"]').val('');
}
else if($('input[name="user_pass"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_pass"]').val('user_pass');
}
if($('input[name="joining_date"]').val()=='joining_date' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="joining_date"]').val('');
}
else if($('input[name="joining_date"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="joining_date"]').val('joining_date');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="POST">
<h2>DB Table</h2>
<p>
Activate the slider if you already have your own database table created!
</p>
<div class="inputCheckBox">
<input type="checkbox" name="table_exist" />
</div>
<p>
Edit here only if you have your own predifined table header names!
</p>
<div class="inputBox">
<input type="text" name="table_name" value="users" required />
<label>Table Name</label>
</div>
<div class="inputBox">
<input type="text" name="user_id" value="user_id" required />
<label>User ID</label>
</div>
<div class="inputBox">
<input type="text" name="user_name" value="user_name" required />
<label>User Name</label>
</div>
<div class="inputBox">
<input type="text" name="user_email" value="user_email" required />
<label>User E-mail</label>
</div>
<div class="inputBox">
<input type="text" name="user_pass" value="user_pass" required />
<label>User Password</label>
</div>
<div class="inputBox">
<input type="text" name="joining_date" value="joining_date" required />
<label>User Added Date</label>
</div>
<button type="submit" name="btn-install">
Install
</button>
</form>
The whole process is to identify if the fields have the default values when a checkbox is unchecked. If the state is changed to checked then empty all fields to input custom information/values. If unchecked again it returns the initial values.
$(document).ready(function(){
$('input[name="table_exist"]').click(function(){
if($('input[name="table_name"]').val()=='users' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="table_name"]').val('');
}
else if($('input[name="table_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="table_name"]').val('users');
}
if($('input[name="user_id"]').val()=='user_id' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_id"]').val('');
}
else if($('input[name="user_id"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_id"]').val('user_id');
}
if($('input[name="user_name"]').val()=='user_name' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_name"]').val('');
}
else if($('input[name="user_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_name"]').val('user_name');
}
if($('input[name="user_email"]').val()=='user_email' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_email"]').val('');
}
else if($('input[name="user_email"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_email"]').val('user_email');
}
if($('input[name="user_pass"]').val()=='user_pass' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_pass"]').val('');
}
else if($('input[name="user_pass"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_pass"]').val('user_pass');
}
if($('input[name="joining_date"]').val()=='joining_date' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="joining_date"]').val('');
}
else if($('input[name="joining_date"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="joining_date"]').val('joining_date');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="POST">
<h2>DB Table</h2>
<p>
Activate the slider if you already have your own database table created!
</p>
<div class="inputCheckBox">
<input type="checkbox" name="table_exist" />
</div>
<p>
Edit here only if you have your own predifined table header names!
</p>
<div class="inputBox">
<input type="text" name="table_name" value="users" required />
<label>Table Name</label>
</div>
<div class="inputBox">
<input type="text" name="user_id" value="user_id" required />
<label>User ID</label>
</div>
<div class="inputBox">
<input type="text" name="user_name" value="user_name" required />
<label>User Name</label>
</div>
<div class="inputBox">
<input type="text" name="user_email" value="user_email" required />
<label>User E-mail</label>
</div>
<div class="inputBox">
<input type="text" name="user_pass" value="user_pass" required />
<label>User Password</label>
</div>
<div class="inputBox">
<input type="text" name="joining_date" value="joining_date" required />
<label>User Added Date</label>
</div>
<button type="submit" name="btn-install">
Install
</button>
</form>
$(document).ready(function(){
$('input[name="table_exist"]').click(function(){
if($('input[name="table_name"]').val()=='users' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="table_name"]').val('');
}
else if($('input[name="table_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="table_name"]').val('users');
}
if($('input[name="user_id"]').val()=='user_id' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_id"]').val('');
}
else if($('input[name="user_id"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_id"]').val('user_id');
}
if($('input[name="user_name"]').val()=='user_name' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_name"]').val('');
}
else if($('input[name="user_name"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_name"]').val('user_name');
}
if($('input[name="user_email"]').val()=='user_email' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_email"]').val('');
}
else if($('input[name="user_email"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_email"]').val('user_email');
}
if($('input[name="user_pass"]').val()=='user_pass' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_pass"]').val('');
}
else if($('input[name="user_pass"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="user_pass"]').val('user_pass');
}
if($('input[name="joining_date"]').val()=='joining_date' && $('input[name="table_exist"]').is(':checked'))
{
$('input[name="joining_date"]').val('');
}
else if($('input[name="joining_date"]').val()=='' && !$('input[name="table_exist"]').is(':checked'))
{
$('input[name="joining_date"]').val('joining_date');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="POST">
<h2>DB Table</h2>
<p>
Activate the slider if you already have your own database table created!
</p>
<div class="inputCheckBox">
<input type="checkbox" name="table_exist" />
</div>
<p>
Edit here only if you have your own predifined table header names!
</p>
<div class="inputBox">
<input type="text" name="table_name" value="users" required />
<label>Table Name</label>
</div>
<div class="inputBox">
<input type="text" name="user_id" value="user_id" required />
<label>User ID</label>
</div>
<div class="inputBox">
<input type="text" name="user_name" value="user_name" required />
<label>User Name</label>
</div>
<div class="inputBox">
<input type="text" name="user_email" value="user_email" required />
<label>User E-mail</label>
</div>
<div class="inputBox">
<input type="text" name="user_pass" value="user_pass" required />
<label>User Password</label>
</div>
<div class="inputBox">
<input type="text" name="joining_date" value="joining_date" required />
<label>User Added Date</label>
</div>
<button type="submit" name="btn-install">
Install
</button>
</form>
javascript jquery form
javascript jquery form
edited Dec 26 '18 at 18:02
user1286956
asked Dec 26 '18 at 14:15
user1286956user1286956
174
174
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
There're multiple ways to do that. You should use a loop.
I've wrote a small script, that would do the job. I do not know if the if block is correct but you should easily fix that with this base.
$(document).ready(function(){
$('input[name="table_exist"]').click(function(){
var table_exist_checked = $('input[name="table_exist"]').is(':checked');
var fields = [
'table_name',
'user_id',
'user_name',
'user_email',
'user_pass',
'joining_date',
]; // Your field names
// iterate over all fieldnames
for(var index of fields) {
var fieldName = fields[index]; // extract current fieldName
var $elem = $('input[name="' + fieldName + '"]'); // save current element so we do not need to repeat
// This is ur if-else. I do not exactly know if this is correct
if($elem.val() == fieldName && table_exist_checked){
$elem.val('');
} else if($elem.val()=='' && !table_exist_checked){
$elem.val(fieldName);
}
}
}); });
$endgroup$
add a comment |
$begingroup$
Following @ArayniMax example I fixed it this way:
$(document).ready(function(){
$('input[name="table_exist"]').click(function(){
var inputCheck = $('input[name="table_exist"]').is(':checked');
var fields = {
'table_name': 'users',
'user_id': 'user_id',
'user_name': 'user_name',
'user_email': 'user_email',
'user_pass': 'user_pass',
'joining_date': 'joining_date'
};
$.each(fields, function(key, value) {
var fieldInput = $('input[name="' + key + '"]');
if(fieldInput.val()==value && inputCheck)
{
fieldInput.val('');
}
else if(fieldInput.val()=='' && !inputCheck)
{
fieldInput.val(value);
}
});
});
});
Thank you for your help everyone!
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210361%2fjquery-code-to-detect-whether-form-fields-have-the-default-value-when-a-checkbox%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
There're multiple ways to do that. You should use a loop.
I've wrote a small script, that would do the job. I do not know if the if block is correct but you should easily fix that with this base.
$(document).ready(function(){
$('input[name="table_exist"]').click(function(){
var table_exist_checked = $('input[name="table_exist"]').is(':checked');
var fields = [
'table_name',
'user_id',
'user_name',
'user_email',
'user_pass',
'joining_date',
]; // Your field names
// iterate over all fieldnames
for(var index of fields) {
var fieldName = fields[index]; // extract current fieldName
var $elem = $('input[name="' + fieldName + '"]'); // save current element so we do not need to repeat
// This is ur if-else. I do not exactly know if this is correct
if($elem.val() == fieldName && table_exist_checked){
$elem.val('');
} else if($elem.val()=='' && !table_exist_checked){
$elem.val(fieldName);
}
}
}); });
$endgroup$
add a comment |
$begingroup$
There're multiple ways to do that. You should use a loop.
I've wrote a small script, that would do the job. I do not know if the if block is correct but you should easily fix that with this base.
$(document).ready(function(){
$('input[name="table_exist"]').click(function(){
var table_exist_checked = $('input[name="table_exist"]').is(':checked');
var fields = [
'table_name',
'user_id',
'user_name',
'user_email',
'user_pass',
'joining_date',
]; // Your field names
// iterate over all fieldnames
for(var index of fields) {
var fieldName = fields[index]; // extract current fieldName
var $elem = $('input[name="' + fieldName + '"]'); // save current element so we do not need to repeat
// This is ur if-else. I do not exactly know if this is correct
if($elem.val() == fieldName && table_exist_checked){
$elem.val('');
} else if($elem.val()=='' && !table_exist_checked){
$elem.val(fieldName);
}
}
}); });
$endgroup$
add a comment |
$begingroup$
There're multiple ways to do that. You should use a loop.
I've wrote a small script, that would do the job. I do not know if the if block is correct but you should easily fix that with this base.
$(document).ready(function(){
$('input[name="table_exist"]').click(function(){
var table_exist_checked = $('input[name="table_exist"]').is(':checked');
var fields = [
'table_name',
'user_id',
'user_name',
'user_email',
'user_pass',
'joining_date',
]; // Your field names
// iterate over all fieldnames
for(var index of fields) {
var fieldName = fields[index]; // extract current fieldName
var $elem = $('input[name="' + fieldName + '"]'); // save current element so we do not need to repeat
// This is ur if-else. I do not exactly know if this is correct
if($elem.val() == fieldName && table_exist_checked){
$elem.val('');
} else if($elem.val()=='' && !table_exist_checked){
$elem.val(fieldName);
}
}
}); });
$endgroup$
There're multiple ways to do that. You should use a loop.
I've wrote a small script, that would do the job. I do not know if the if block is correct but you should easily fix that with this base.
$(document).ready(function(){
$('input[name="table_exist"]').click(function(){
var table_exist_checked = $('input[name="table_exist"]').is(':checked');
var fields = [
'table_name',
'user_id',
'user_name',
'user_email',
'user_pass',
'joining_date',
]; // Your field names
// iterate over all fieldnames
for(var index of fields) {
var fieldName = fields[index]; // extract current fieldName
var $elem = $('input[name="' + fieldName + '"]'); // save current element so we do not need to repeat
// This is ur if-else. I do not exactly know if this is correct
if($elem.val() == fieldName && table_exist_checked){
$elem.val('');
} else if($elem.val()=='' && !table_exist_checked){
$elem.val(fieldName);
}
}
}); });
$(document).ready(function(){
$('input[name="table_exist"]').click(function(){
var table_exist_checked = $('input[name="table_exist"]').is(':checked');
var fields = [
'table_name',
'user_id',
'user_name',
'user_email',
'user_pass',
'joining_date',
]; // Your field names
// iterate over all fieldnames
for(var index of fields) {
var fieldName = fields[index]; // extract current fieldName
var $elem = $('input[name="' + fieldName + '"]'); // save current element so we do not need to repeat
// This is ur if-else. I do not exactly know if this is correct
if($elem.val() == fieldName && table_exist_checked){
$elem.val('');
} else if($elem.val()=='' && !table_exist_checked){
$elem.val(fieldName);
}
}
}); });
$(document).ready(function(){
$('input[name="table_exist"]').click(function(){
var table_exist_checked = $('input[name="table_exist"]').is(':checked');
var fields = [
'table_name',
'user_id',
'user_name',
'user_email',
'user_pass',
'joining_date',
]; // Your field names
// iterate over all fieldnames
for(var index of fields) {
var fieldName = fields[index]; // extract current fieldName
var $elem = $('input[name="' + fieldName + '"]'); // save current element so we do not need to repeat
// This is ur if-else. I do not exactly know if this is correct
if($elem.val() == fieldName && table_exist_checked){
$elem.val('');
} else if($elem.val()=='' && !table_exist_checked){
$elem.val(fieldName);
}
}
}); });
answered Dec 26 '18 at 16:40
ArayniMaxArayniMax
362
362
add a comment |
add a comment |
$begingroup$
Following @ArayniMax example I fixed it this way:
$(document).ready(function(){
$('input[name="table_exist"]').click(function(){
var inputCheck = $('input[name="table_exist"]').is(':checked');
var fields = {
'table_name': 'users',
'user_id': 'user_id',
'user_name': 'user_name',
'user_email': 'user_email',
'user_pass': 'user_pass',
'joining_date': 'joining_date'
};
$.each(fields, function(key, value) {
var fieldInput = $('input[name="' + key + '"]');
if(fieldInput.val()==value && inputCheck)
{
fieldInput.val('');
}
else if(fieldInput.val()=='' && !inputCheck)
{
fieldInput.val(value);
}
});
});
});
Thank you for your help everyone!
$endgroup$
add a comment |
$begingroup$
Following @ArayniMax example I fixed it this way:
$(document).ready(function(){
$('input[name="table_exist"]').click(function(){
var inputCheck = $('input[name="table_exist"]').is(':checked');
var fields = {
'table_name': 'users',
'user_id': 'user_id',
'user_name': 'user_name',
'user_email': 'user_email',
'user_pass': 'user_pass',
'joining_date': 'joining_date'
};
$.each(fields, function(key, value) {
var fieldInput = $('input[name="' + key + '"]');
if(fieldInput.val()==value && inputCheck)
{
fieldInput.val('');
}
else if(fieldInput.val()=='' && !inputCheck)
{
fieldInput.val(value);
}
});
});
});
Thank you for your help everyone!
$endgroup$
add a comment |
$begingroup$
Following @ArayniMax example I fixed it this way:
$(document).ready(function(){
$('input[name="table_exist"]').click(function(){
var inputCheck = $('input[name="table_exist"]').is(':checked');
var fields = {
'table_name': 'users',
'user_id': 'user_id',
'user_name': 'user_name',
'user_email': 'user_email',
'user_pass': 'user_pass',
'joining_date': 'joining_date'
};
$.each(fields, function(key, value) {
var fieldInput = $('input[name="' + key + '"]');
if(fieldInput.val()==value && inputCheck)
{
fieldInput.val('');
}
else if(fieldInput.val()=='' && !inputCheck)
{
fieldInput.val(value);
}
});
});
});
Thank you for your help everyone!
$endgroup$
Following @ArayniMax example I fixed it this way:
$(document).ready(function(){
$('input[name="table_exist"]').click(function(){
var inputCheck = $('input[name="table_exist"]').is(':checked');
var fields = {
'table_name': 'users',
'user_id': 'user_id',
'user_name': 'user_name',
'user_email': 'user_email',
'user_pass': 'user_pass',
'joining_date': 'joining_date'
};
$.each(fields, function(key, value) {
var fieldInput = $('input[name="' + key + '"]');
if(fieldInput.val()==value && inputCheck)
{
fieldInput.val('');
}
else if(fieldInput.val()=='' && !inputCheck)
{
fieldInput.val(value);
}
});
});
});
Thank you for your help everyone!
edited Dec 26 '18 at 18:50
answered Dec 26 '18 at 18:22
user1286956user1286956
174
174
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210361%2fjquery-code-to-detect-whether-form-fields-have-the-default-value-when-a-checkbox%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown