开发者

setting values [jquery or javascript]

开发者 https://www.devze.com 2023-02-24 03:22 出处:网络
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

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 forms:

$('input:text[name="q"]').keypress(
    function(e){
        $('#askCenter').find('textarea').val($(this).val());
    });


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


<input type="text" id="txt1" onkeyup="document.getElementById('txt2').value=this.value;"/>
<input type="text" id="txt2" />
0

精彩评论

暂无评论...
验证码 换一张
取 消