L
L
LebedevStr2017-05-26 16:39:21
JavaScript
LebedevStr, 2017-05-26 16:39:21

How to start a piece of jQuery code?

Hello. There is a piece of code

jQuery(function() {
  jQuery(".fancybox").fancybox();
    jQuery('.speaker').text(jQuery('.speaker').data('text'));
    jQuery('.speaker').click( function () {
            var speakerText = jQuery(this).closest(".row").text();
            
            if(jQuery(this).data('lang') == 'RU') {
                var speakerLang = 'Russian Female';
            } else {
                var speakerLang = 'Polish Female';
            }

            if(jQuery("#resume").hasClass("speaker_play")) {
                responsiveVoice.resume();
                jQuery(".speaker_play").removeAttr("id");
                
            } else {
            responsiveVoice.speak(speakerText, speakerLang,  {rate: jQuery(this).data('rate')});
        };
            jQuery('.speaker_play').hide();
            jQuery('.speaker').show();
            jQuery(this).hide();
            jQuery('.speaker_play').text(jQuery('.speaker_play').data('text'));
            jQuery(this).closest(".speaker__inner").children('.speaker_play').show();
    });

    jQuery('.speaker_play').click( function () {
        responsiveVoice.pause();
        jQuery(this).hide();
        jQuery(this).closest(".speaker__inner").children('.speaker').show();
        jQuery('.speaker_play').attr('id', 'resume');
    });

});


The function is to send data to the audio "talker" via the API, the script is quite working without checking in line 13. But I need the check in order not to reset the speech reset, but to resume if there was an earlier click on the Stop button ( id="resume"). Help :)

if(jQuery("#resume").hasClass("speaker_play"))


Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
You2, 2018-08-31
@You2

1e - Replacing the id is usually not the best practice. Much better to remake under classes.
Other than that - add an intermediate class or attribute, add/remove it after clicking.
For example, after clicking, add the class speaker_play_clicked / attribute attr-speaker-clicked="1"
As a result, the check will be:
if(jQuery("#resume").hasClass("speaker_play_clicked")) / if(jQuery("#resume") .attr("attr-speaker-clicked")==1)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question