0

So I have an input box, and when a user types something in it I want a second textarea change its value to match that of the input's, on click. How can I do this using javascript, jquery, or something simpler, or php...

Here's my code:

This is the first input:

<form class="form1" action=../search.php method="post">
<input class="askInput" type="text" name="q" value="Search for tags and users or ask a question" onclick="if(this.value == 'Search for tags and users or ask a question') this.value='';"  onblur="if(this.value.length == 0) this.value='Search for tags and users or ask a question';"></input>
                    <input class="searchEnter" type="image" name="submit" src="../Images/askQuestion.png"></input></form>

This is the textarea:

<div id="askCenter">
<img class="close" src="../Images/closeAsk.png"></img>
<h1><img src="../Images/askQuestionTitle.png" alt="Ask this question"></img></h1>
<textarea></textarea>
<span class="holder"><input type="text" value="add tags" onclick="if(this.value == 'add tags') this.value='';"  onblur="if(this.value.length == 0) this.value='add tags';"></input>
<span class="note">&#8727; Tags are separated by commas</span>

</span>
<input class="askAway" type="image" src="../Images/askAway.png" alt="Ask away"/>

4
  • What mark-up are you working with? Commented Apr 11, 2011 at 23:04
  • php and html is i understand what your asking Commented Apr 11, 2011 at 23:05
  • Not really, I'm asking what the html of your form element looks like. The rendered html, as seen by the browser, since that's what JavaScript (and, jQuery is only JavaSCript) works on, and with. Commented Apr 11, 2011 at 23:06
  • The user types into the input of name="q", and that text should appear in the textarea inside of the div of id="askCenter"? Commented Apr 11, 2011 at 23:21

3 Answers 3

5

Without seeing your mark-up, something like this would work, albeit it's not tailored to your needs:

$('#first').keypress(
    function(e){
        var string = $(this).val();
        $('#textarea').val(string);
    });

$('form').submit(
    function(){
        return false;
    });

JS Fiddle demo.


With your updated question, with the html from your forms:

$('input:text[name="q"]').keypress(
    function(e){
        $('#askCenter').find('textarea').val($(this).val());
    });
Sign up to request clarification or add additional context in comments.

2 Comments

I'd use a different variable name. But +1 for generality!
@hubrid, you're very welcome, glad to have been of service :)
0

TRY IT HERE

HTML MARKUP

<input type="text" id="inputField"></textarea>
<textarea id="textArea"></textarea>

JQUERY

$(document).ready(function(){
    $('#inputField').keyup(function() {
        $('#textArea').val($(this).val());
    });
});

3 Comments

Could you bring the relevant jQuery, or JavaScript, in-line with your answer?
sweeeeeeeeeeeeet. (had to have lots of characters to reach the limit)
I'd go easy with the caps lock key ;)
0
<input type="text" id="txt1" onkeyup="document.getElementById('txt2').value=this.value;"/>
<input type="text" id="txt2" />

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.