0
votes

I want my textbox and textarea to be import base on selected value, for example, on my select box, there are options subject A and subject B, when I choose subject B, the textbox will automatically insert subject B and the textarea will import the preset message which is stored in database.

Currently I have success imported message by selected option, how can I insert subject to textbox as well when I click button?

JQuery:

$(document).ready(function() {
    $("#copyBtn").click(function(){
        $("#selmessage").val($("#selectBox").val());
    });
});

PHP:

<input type="Text" name="seltitle" value="<?=$the_title;?>">&nbsp;&nbsp;
        <select id="selectBox" name="seltitle2">
            <option selected></option>
            <?php
            $q = "SELECT * FROM template ORDER BY preset_subj ASC";
            $result = $mysqli->query($q) or die($mysqli->error);

            while($row = $result->fetch_array(MYSQLI_BOTH)){
            ?>
            <option value="<?php echo $row['message'] ?>"><?php echo $row['preset_subj']; } ?></option>
        </select>
        &nbsp;&nbsp;<input id="copyBtn" type="button" value="import to message" />

<textarea name="selmessage" id="selmessage"></textarea>
2

2 Answers

0
votes

Here is a plan:

(1) You think you need to detect the "click" event, but probably much better in the "change" event of the select box! Here is a good article how to handle form elements' "change" event (also see the comments - very useful): http://jivebay.com/handling-checkboxes-radio-buttons-and-select-options-in-jquery/

(2) When you detect the change, then you need to get the selected value (also find it in the above link) and set the value of the textarea - this is easy: $("#textarea_1").val( selectedValue );

I hope you've got the idea. Let me know if you need anything else.

0
votes
$(document).ready(function() {
    $("#copyBtn").click(function(event){
        event.preventDefault();
        $("#selmessage").val($("#selectBox option:selected").val());
    });
});

or you can apply filter in here also

$(document).ready(function(event) {
    $("#copyBtn").click(function(){
        event.preventDefault();
        selected = $("#selectBox option:selected").val();
        if(selected != ''){
            $("#selmessage").val($("#selectBox option:selected").val());
        }
    });
});