tags:

views:

34

answers:

4

Hi all,

I am stuck over a question in which I got to make a Hidden Text box to store a value.
But I have seen that JQuery is capable to add some value to a Hidden Text box.
I am not sure which is the best way to hide the value which will be stored to the Database.
Please look at the codes below:

Traditional way:

<input type="hidden" id="data[Test][quote]" value="hello" />

Using JQuery:

$(document).ready(function(){
 $("#data\\[Test\\]\\[quote\\]").val("hello"); 
});       
<input type="hidden" id="data[Test][quote]" />

Please advise.

+3  A: 

If you can do it without JavaScript / jQuery, do it without.

John McCollum
Good simple answer. You have my +1 atleast. but people like complex solution even to simple problems.
simplyharsh
+1  A: 

Not necessarily, you can even use raw javascript for that at least:

window.onload = function(){
  var el = document.getElementById("data\\[Test\\]\\[quote\\]");
  el.value = 'Hello';
};
Sarfraz
+1  A: 

Using jQuery you don't need to use a hidden field, use jQuerys.data()` method.

$(document).ready(function(){
    $.data(document.body, 'data', 'hello');
    alert($.data(document.body, 'data'));
});
jAndy
+1  A: 
  1. Unless the parameter has to change on-the-fly, don't use jQuery to do something already possible with plain old HTML.
  2. That is not a hidden text box; it's just a tag to include some input data without rendering any form control on the browser.
BoltClock