Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery dynamic adding hidden field

Tags:

jquery

$().ready(function() 
  {
  $("#add").click(function() 
    {
    var vals = $("#txtaddfeature").val();
    if(vals !='')
      $('#FeatureLists').prepend('<option value="' + vals + '" selected="selected">' + vals + '</option>');
    $('#txtaddfeature').val('');
    });
  });

Ok after adding the value to the select list as above

$('#FeatureLists').prepend('<option value="' + vals + '" selected="selected">' + vals + '</option>');

I want to create a dynamic hidden field with id=vals defined above and set it value to value entered in the textbox . how can i do that

like image 807
maztt Avatar asked May 14 '10 12:05

maztt


1 Answers

I couldn't tell exactly what you wanted. It seems like you want both the ID and the value of the new hidden input to be the value of vals. Is that right?

var $hiddenInput = $('<input/>',{type:'hidden',id:vals,value:vals});

Then you would append it wherever you want.

$hiddenInput.appendTo(selector);

EDIT:

For clarification, selector is the reference to the element where you want to append your new input.

If you want to append it to the body tag, do:

$hiddenInput.appendTo('body');

If you want to append it to an element with the class someClass, do:

$hiddenInput.appendTo('.someClass');
like image 161
user113716 Avatar answered Sep 24 '22 22:09

user113716