7
$().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

2
  • Off topic - What version of jQuery are you using? $().ready(...) probably shouldn't be used if you have jQuery 1.4 since $() no longer returns a jQuery object with the document. Instead, do $(document).ready(...), or just $(function() {...}). Commented May 14, 2010 at 12:20
  • ok thx I will review my code and change it Commented May 14, 2010 at 12:26

3 Answers 3

20

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');
Sign up to request clarification or add additional context in comments.

3 Comments

$hiddenInput.appendTo(selector); , what is the selector here
Sorry, selector is simply the element where you want to append the input. For example, if you typed 'body', it would append the hidden input to the body. I'll update my answer.
Why the '$' on hiddenInput?
2

I would check to see if it was on the page first, then add.

function SetHiddenInput(val)
{
    var $txtaddfeaturehidden == $("#txtaddfeaturehidden");

    if ($txtaddfeaturehidden.length == 0)
    {
        $("input").attr({
            id : "txtaddfeaturehidden", 
            type : "hidden",
            value : $('#txtaddfeature').val()
        }).after('#txtaddfeature');
    }
    else $txtaddfeaturehidden.val(val);
}

Comments

1

You can do like:

$("input").attr("type", 'hidden').val($('#txtaddfeature').val()).appendTo('selector_here');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.