2

I'm experimenting with a code that is generating phrases with select that you can edit in order the change the meaning of the phrase. I would like to add the word "est" to a class so it's displayed in the page with a different font than the other words. Actually my code is repeating the div .un each time there is a word.

How do I tell the code that if the option has a class ="something" apply to its specific .un another style?

I don't want to edit the style in the list (because as you can see, it's already done), I would like to change the style of the displayed result. I would like the word "est" to be displayed with the same style as it is in the list.

here is my code :

 $(document).ready(function() {

                        });

                        let phrases = ["il n'est pas. "

                        ];
                        let lettres = /[\u0041-\u005a\u0061-\u007a\u00aa-\u00b5\u00ba-\u00c0\u00c1-\u00d6\u00d8-\u00f6]/gui;

                        tokenize = function(phrase) {
                            let mots = [''];
                            let it = phrase[Symbol.iterator]();
                            for (var c = it.next(); !c.done; ) {

                                for (; !c.done && c.value.match(lettres); c = it.next()) {
                                    mots[mots.length-1]+=(c.value);
                                }
                            //console.log(c.value);
                            if (mots[mots.length-1].length != 0 ){
                                mots.push('');
                            }

                            if (c.value == ' ') {
                                for (c = it.next(); !c.done && c.value == " "; c = it.next()) {continue;} continue;
                            } 
                                // console.log(i);
                                
                                console.log(mots);

                                if (!c.value.match(lettres)){
                                    mots[mots.length-1]+=(c.value);
                                //console.log(c.value);
                                mots.push('');
                            }
                            c = it.next();
                        }
                        return mots.slice(0, mots.length-1);

                    
                    }



                    $(document).ready(function() {
                        let LARGEUR = $("#container .repeat").clone();
                        let change=function(){
                            $(".width_tmp_option", this.parent).html($('option:selected', this).val());
                            $(this).width($(".width_tmp_select", this.parent).width());
                        }
                        $('#container').on("change",".un",change);


                        let idx = Math.floor(Math.random() * Math.floor(phrases.length));
                        let mots = tokenize(phrases[idx]);

                        for( var i = 0 ; i < mots.length-1; i++){
                            $('#container .repeat:last-child').after(LARGEUR.clone());}
                            var i = 0;
                            console.log(mots.length);
                            $('#container .repeat').each(function(){
                                $('.un', this).val(mots[i]).each(change);
                                i++;

                            });

           

                            $('select').on('change', function() {
                             let phrase = $('.un').get().reduce((a,b) => a+' '+b.value, '').substring(1).trim(); 
                             console.log(phrase);
                            if (phrase.includes('il est bleu')) {
                                $(".white").css('background-color', 'blue');
                            } else {
                                 $(".white").css('background-color', 'white');
                                }
                           
                            
                 });
            });
 .something{
        color:red;
        font-family: cursive;
    }

    body {
        width: 70vw;
        height: 100vh;
        overflow: normal;
        overflow: hidden;


    }

    .un{
        width: 2rem;
        margin: 0.2rem;
        font-family: sans-serif;
        font-size: 2.5rem; 
    } 


    .un * {

        border-radius: 15px;

        font-size: 2.5rem; 
    }

    option {
        background-color: none;

    }

    option:hover {
        background-color: green;

    }

    ::selection {
        background-color: green;
    }

    .width_tmp_select{
        background-color: none;

    }


    .width_tmp_select{
        display : none;
        font-size: 2.5rem; 
        font-family: 'Rungli-Italic';
    } 


    #p1{
        font-size: 2rem;
        border: none;
    } 



    /* For IE10 */
    select::-ms-expand {
        display: none;

    }

    .repeat {
        display: inline-block;

    }

    .continuer {
        opacity: 0.2;
        position: absolute;
        bottom: 0.5vw;
        right: 0.5vw;
        font-size: 2.5rem; 
        padding: 0.1vw;
    }

    .continuer:hover {
        opacity: 1;

        background-color: white;

    }
<script
        src="https://code.jquery.com/jquery-3.4.1.min.js"
        integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
        crossorigin="anonymous"></script>

    <body class="white">

        <div id="container">
            <div class="repeat">

                <select name="mots" class="un">
                    <option  value="il">il</option>
                    <option  class="something" value="est">est</option>  // I would like this one to have another style that the rest of the phrase.
                    <option value="bleu">bleu</option>
                    <option value="n">n</option>
                    <option value="'">'</option>
                    <option value="pas">pas</option>
                    <option value=".">.</option>

                </select>
                <select class="width_tmp_select">
                    <option class="width_tmp_option"></option>
                </select>
            </div>
        </div>

    </body>
Aurore
  • 696
  • 4
  • 16

1 Answers1

0

You can check class for selected option inside $('select').change function

$('select').on('change', function () {
let phrase = $('.un').get().reduce((a, b) => a + ' ' + b.value, '').substring(1).trim();
console.log(phrase);
var selectedOption = $(this).find('option:selected');
var optionClass = selectedOption.attr('class');//here you can get option class
if(optionClass === "something"){
  //do stuff here
}
if (phrase.includes('il est bleu')) {
    $(".white").css('background-color', 'blue');
} else {
    $(".white").css('background-color', 'white');
}
});

btw you applied change event for all drop downs so $(this).find('option:selected') will get you the current selected option for the current drop down

Yehia Elhawary
  • 578
  • 1
  • 4
  • 20
  • How am I suppposed to make this work? I'm sorry but this is new to me. – Aurore Aug 09 '20 at 12:23
  • i updated answer, as you see i put if condition if selected option class match 'something' class – Yehia Elhawary Aug 09 '20 at 13:33
  • it works, but its not doing what I asked for... I would like to be ablet to change the style for ONLY the element that has the class "something" please, can you read my question above? I edited it, so it's maybe clearer... – Aurore Aug 09 '20 at 14:59
  • my issue would be: how to get the same style that "est" is having in the dropdown list but also when selected? – Aurore Aug 09 '20 at 15:02