0

This should be easy, but nothing seems to work. I have a jQuery function that contains two parameters. The first parameter (pageID) is supposed to be inserted into a hidden field. It works correctly. I want the second parameter to be inserted into an html input field. That is, I want to see the contents of the parameter 'pageName' inside the form field as if I had just typed it in myself. Here is the code:

function insertIntoHiddenField(pageID,pageName)
{
    $('input[name=page_id]').val(pageID);
    $('input[name=page_name]').val(pageName);
}

When I look at the source code, I find that the value attribute in the input tag contains the value from the parameter 'pageName'. But, the input field itself is empty. I want to see that value in the input field. What am I doing wrong?

By the way, I also tried (without success) the following:

$('input[name=page_name]').text(pageName);
$('input[name=page_name]').innerhtml(pageName);
$('input[name=page_name]').append(pageName);
1
  • 1
    Can you post a demo to reproduce this problem? Because your code looks like it should work, so I strongly suspect that something else is happening. Commented Mar 18, 2012 at 7:57

2 Answers 2

2

Give an id to the hidden element. Just to be on the safe side and use .val()

$('#page_name').val('newtext');
$('#page_id').val('newtext');

Demo

However, your method works perfectly for me. Check here

Sign up to request clarification or add additional context in comments.

4 Comments

That's not really the safe side. That's the only side ;)
and how this answers his question ?
@RoyiNamir, It provides an alternative to his how to add text question and tell him that his method are also working at the same time.
Thanks! Using an id instead of the name attribute made it work. Not sure why, but it works!
0

Check your input html is valid, this example works.

<input name=​"page_name" type=​"text" value=​"">​

$("input[name=page_name]").val() == "" // true
$("input[name=page_name]").val("a")
$("input[name=page_name]").val() == "a" // true

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.