简体   繁体   中英

setting values [jquery or javascript]

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"/>

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 form s:

$('input:text[name="q"]').keypress(
    function(e){
        $('#askCenter').find('textarea').val($(this).val());
    });
<input type="text" id="txt1" onkeyup="document.getElementById('txt2').value=this.value;"/>
<input type="text" id="txt2" />

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());
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM