Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery - Show/Hide a hidden input form field

I'm new to Javascript and trying to understand it better. I have a form which is generated by php, using data from POST. The form has some hidden form fields, which are supposed to be populated with values after validation.

The relevant html code:

<form action="" method="post" name="FormProcessor">
<b>Domain Name: </b>
<input type="text" name="MAIN_DOMAINNAME" value="" id="DomainField">
<input type="hidden" name="CONF_FILE" value="" id="ConfFile">
<div id="infomsg">

Javascript code:

$(document).ready (function()
{
    $('#DomainField').blur(function() {
        var DomField=$("#DomainField");
        var DomText=DomField.val();     
        var fold="/var/lib/bind/db.";
        alert(fold+DomText);
        var ConfFile=$("#ConfFile");
        ConfFile.val(fold+DomText);
        ConfFile.show();
    });
});

I'm trying to get the second <input> field to be 'unhidden' when the focus of the previous field is lost. The function gets executed, and the alert is shown.

On checking the source, I can see that it shows:

<input type="hidden" id="ConfFile" value="/var/lib/bind/db.g.com" name="CONF_FILE" style="display: inline;">

So the value is propogated, so I did address the object properly. Why isnt it becoming shown?

like image 929
Joel G Mathew Avatar asked Jul 24 '13 06:07

Joel G Mathew


People also ask

How do you make an input element invisible?

The <input type="hidden"> defines a hidden input field. A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted.

How can store hidden field value in jQuery?

In jQuery to set a hidden field value, we use . val() method. The jQuery . val() method is used to get or set the values of form elements such as input, select, textarea.

How do you hide a field in JavaScript?

Style display property is used to hide and show the content of HTML DOM by accessing the DOM element using JavaScript/jQuery. To hide an element, set the style display property to “none”. document. getElementById("element").


1 Answers

A hidden input field is supposed to remain hidden.

What I think you want to do is use a normal input field of the type text and hide it using css. Then you are able to show it using jQuery the way you are doing it now.

It should work if you replace your hidden field with:

<input type="text" name="CONF_FILE" value="" id="ConfFile" style="display:none">
like image 86
Jacob Oettinger Avatar answered Oct 03 '22 14:10

Jacob Oettinger