0

Note: I have a situation where i can't set the value in hidden input element.

var size_val = $("#size_val").val();
var size_measure = $("#size_measure").val();
$("#size_val").val(size_val+' '+size_measure);

I want to set the value for the same textbox 'size_val' but don't want to show the new value on the page.

Note: I know, i can set the value in hidden field but i've a situation where i can't set in hidden field.

Avnish Tiwary
  • 2,188
  • 22
  • 27
  • 2
    but you are setting value using $("#size_val"), then definitely it will definitely set value $("#size_val"). where is your hidden field ? – Pedram May 31 '16 at 11:30
  • @pedram I want to set the value for the same textbox 'size_val' but don't want to show the value in that textbox. – Avnish Tiwary May 31 '16 at 11:32
  • You want to chage the value of an `` but you don't want to show it? –  May 31 '16 at 11:37
  • @LutzHorn Yes bro. – Avnish Tiwary May 31 '16 at 11:40
  • An `input type="text"` has no hidden value. What do you have in mind? –  May 31 '16 at 11:42
  • 1
    @AvnishTiwary You can't change the value and display an other value. You can use data attribute example `data-value='your value'`. – Alexis May 31 '16 at 11:42
  • @Alexis With the help of data-value, can i set the new value for the text field and also would the value not show in the textbox? – Avnish Tiwary May 31 '16 at 11:47
  • @Alexis - Now what you have noticed on below answer? Please check! Someone has copied my answer. – Pedram May 31 '16 at 11:48
  • Possible duplicate of [Set value of hidden input with jquery](https://stackoverflow.com/questions/4802999/set-value-of-hidden-input-with-jquery) – Marcello B. Sep 07 '19 at 19:51

1 Answers1

4

Well, that's because you are setting the value on the #size_val element.

You need to set the value on the hidden element you want, like this:

var size_val = $("#size_val").val();
var size_measure = $("#size_measure").val();
$("#id_of_hidden_element").val(size_val+' '+size_measure);

UPDATE BASED ON OP EDIT

So, if I understand correctly what you need is not setting a value on a hidden field but to set a hidden value on a field. We can do this with attributes on that field:

Custom attribute:

var size_val = $("#size_val").val();
var size_measure = $("#size_measure").val();
$("#size_val").attr("hiddenvalue",size_val+' '+size_measure);

To recover that value you can do this:

var hidden_value = $("#size_val").attr("hiddenvalue");

The html input would look like this:

<input type="text" value="showed value" hiddenvalue="hided value"/>

Data attribute:

var size_val = $("#size_val").val();
var size_measure = $("#size_measure").val();
$("#size_val").data("hiddenvalue",size_val+' '+size_measure);

The html input would look like this:

<input type="text" value="showed value" data-hiddenvalue="hided value"/>

To recover that value you can do this:

var hidden_value = $("#size_val").data("hiddenvalue");

Note that the "hiddenvalue" is just a name for the attribute we can use any other name.

ccasado
  • 121
  • 7
  • 2
    I've edited the question. I don't want to set the value in hidden field. – Avnish Tiwary May 31 '16 at 11:38
  • Thx! I made an update that I think fullfill your needs – ccasado May 31 '16 at 11:46
  • It worked for me. But one thing i'd like to ask that can i get the new value on server side by using name attribute. – Avnish Tiwary May 31 '16 at 12:03
  • Depends on server side language and control mangement in templates. For example in ASP.NET if you add the attribute runat="server" you can get on server side every attribute value but I don't think that this practices are common. There would be a few tricks that you can try but I think this is a topic for another question – ccasado May 31 '16 at 15:37