简体   繁体   中英

Detect multiple drop-down selections and paragraph content dynamically with jQuery and JavaScript

I've created a madlib style paragraph with multiple drop-down selections for synonyms of various words. Here's an example:

<p id="the-text">This is an example paragraph containing many
    <select class="selector">
        <option>selections</option>
        <option>dropdown thingies</option>
        <option>option choosers</option>
    </select>that I would like to be able to
    <select class="selector">
        <option>click on</option>
        <option>select</option>
        <option>choose</option>
    </select>and then know what the
    <select class="selector">
        <option>final</option>
        <option>selected</option>
        <option>variable</option>
    </select>paragraph text is.
    <select class="selector">
        <option>It would be great</option>
        <option>It would be nice</option>
        <option>It'd be delightful</option>
    </select>, and
    <select class="selector">
        <option>useful</option>
        <option>helpful</option>
        <option>interesting</option>
    </select>to dynamically create paragraphs like this.</p>
<textarea id="text-area" rows="4" cols="110">This is where the text should appear...     
</textarea>

Here is a live example: http://jsfiddle.net/T4guG/2/

Using jQuery and Javascript, I am trying to get the selected (and surrounding) text to appear in the text area.

It's kind of working, but there are two problems:

1) SOLVED: There was a problem with punctuation, but replacing:

    if (element == "{") {
        content_array[i] = foo[j];
        j++;
    }

with

    if (element.indexOf('{') >= 0) {
        content_array[i] = foo[j];
        j++;
    }

allows { to be detected consistently

2) SOLVED: you only can change the options once.

Is there a more elegant solution than what I have come up with? Here is the code:

function updateTextArea() {
    //get all of the text selections, and put them in an array
    var foo = [];
    $('.selector :selected').each(function (i, selected) {
        foo[i] = $(selected).text();
    });

    //get the paragraph content, and store it
    var safe_content = $('#the-text').html();

    //delete all the options
    $('.selector').text('');

    //get the text without the dropdown options
    var content = $('#the-text').html();

    //create a regex expression to detect the remaining drop-down code
    var pattern = "<select class=\"selector\"></select>",
        re = new RegExp(pattern, "g");

    //replace all the drop-down selections with {
    content = content.replace(re, "{");

    //turn the content into an array
    content_array = content.split(" ");

    //go through the array, and if a element is {, go to "foo" and replace it with the selected option
    var length = content_array.length,
        element = null;
    var j = 0;
    for (var i = 0; i < length; i++) {
        element = content_array[i];

        if (element == "{") {
            content_array[i] = foo[j];
            j++;
        }

    }

    //turn the array back into a paragraph
    new_content = content_array.join(" ");

    //replace the text with the origionanl text
    $('#the-text').html(safe_content);

    //put the new content into the text area
    $('#text-area').val(new_content);
}

$(document).ready(function () {
    updateTextArea();
});

$(".selector").change(function () {
    updateTextArea();
});
  1. You are splitting text based on " " (using space) and replacing element { with array value but text is. {, and text is. {, and contains comma ie, {, is not equal to { . Add space after element { . This solves your first problem.

  2. As you are removing and adding select options dynamically in function updateTextArea() . You have to use .on() to attach event handler for dynamically created elements.

Try:

$( document ).on("change",".selector",function() {
  updateTextArea();
});

Instead of

$(".selector").change(function () {
    updateTextArea();
});

DEMO FIDDLE

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