1

In file main.phtml i have

<a href="<?=$block->getUrl("mpblog/post/history") . "?id=".$_post->getPostId()?>" class="edit-post"><span><?= __("Edit or delete post")?></span></a>

In file post.phtml i have

            foreach ($postCollection as $post) : ?>
                <div data-post-id="<?= $block->escapeHtml($post->getId()); ?>"
                     class="post-list-item col-mp post-item-list mp-12">
                    <div class="post-item-wraper">
                        <?= /* @noEscape */ $block->getStatusHtmlById($post->getEnabled()) ?>
                        <?php if ($post->getImage()):
                            $images = explode("|",$post->getImage())?>
                            <div class="post-image">
                                <a href="<?php echo ($post->getEnabled() == 1) ? $post->getUrl() : 'javascript:void(0)' ?>">
                                    <img class="img-responsive"
                                         src="<?= $block->resizeImage($images[0], '400x'); ?>"
                                         alt="<?= $block->escapeHtml($post->getName()); ?>"/>
                                </a>
                            </div>
                        <?php endif; ?>
                        <div class="post-info-wraper">
                            <h2 class="mp-post-title">
                                <a class="post-link-title" title="<?= $block->escapeHtml($post->getName()); ?>"
                                   href="<?php echo ($post->getEnabled() == 1) ? $post->getUrl() : 'javascript:void(0)' ?>">
                                    <?= $block->escapeHtml($post->getName()); ?>
                                </a>
                            </h2>
                            <div class="mp-post-info">
                                <?= $block->getPostInfo($post); ?>
                            </div>
                            <div class="post-short-description">
                                <p><?= $block->maxShortDescription($post->getShortDescription()); ?></p>
                            </div>
                            <div class="mp-clearfix"></div>
                            <div class="mp-post-meta-info mp-alignright">
                                <div class="actions">
                                    <a class="mp-read-more" href="<?php echo ($post->getEnabled() == 1) ? $post->getUrl() : 'javascript:void(0)' ?>"
                                       title="<?= __('Read More'); ?>">
                                        <?= __('Read More'); ?>
                                    </a>
                                </div>
                            </div>
                        </div>
                        <div class="post-info-action" data-postId="<?= $block->escapeHtml($post->getId()); ?>">
                            <div class="mpblog-post-duplicate">
                                <img src="<?= $this->getViewFileUrl('Mageplaza_Blog::media/images/duplicate.svg') ?>"
                                     align="middle"
                                     alt="<?= $block->escapeHtml(__('Duplicate')); ?>"
                                     title="<?= /* @noEscape */
                                     __('Duplicate') ?>"
                                />
                            </div>
                            <?php $isDisableButton = $block->getDisableEditButton($post->getUpdatedAt());
                            if($isDisableButton):
                            ?>
                            <div class="mpblog-post-edit">
                                <img src="<?= $this->getViewFileUrl('Mageplaza_Blog::media/images/writing.svg') ?>"
                                     align="middle"
                                     alt="<?= $block->escapeHtml(__('Edit')); ?>"
                                     title="<?= /* @noEscape */
                                     __('Edit') ?>"
                                />
                            </div>
                            <?php endif; ?>
                            <div class="mpblog-post-delete">
                                <img src="<?= $this->getViewFileUrl('Mageplaza_Blog::media/images/delete.svg') ?>"
                                     align="middle"
                                     alt="<?= $block->escapeHtml(__('Delete')) ?>"
                                     title="<?= /* @noEscape */
                                     __('Delete') ?>"
                                />
                            </div>
                        </div>
                    </div>
                </div>
                <?php if ($i++ % 2 == 0) : ?>
                    <div class="mp-clear"></div>
                <?php endif; ?>
            <?php endforeach; ?>
            <div class="mp-clear"></div>
        </div>

When i click in the i will have js to trigger('click') to button edit

            var url_string = window.location.href;
            var url = new URL(url_string);
            var id = url.searchParams.get("id");

            
                $(function() {
                    $('body').ready(function() {
                        $('.post-list-item[data-post-id="'+id+'"]').find('.mpblog-post-edit').trigger('click');
                    });
                });
            

But it not working. But if i use hide() it working. Only when i use trigger it not woriking. I don't know. Please help me

9
  • you use typeof operator check this link --- tutorialrepublic.com/faq/… and write , debug code following way. Jul 10 '20 at 4:30
  • i edited my question. but if i use hide() it working but when i use trigger() it not working. Please help me
    – Boll
    Jul 10 '20 at 7:29
  • you if condition add this type if($('.post-list-item[data-post-id="'+id+'"]').length > 0) after try Jul 10 '20 at 8:05
  • i tried but it not working
    – Boll
    Jul 10 '20 at 8:36
  • i edit add code for you see
    – Boll
    Jul 10 '20 at 8:43
1

Hope this will help you.

 var url = window.location.href;
 var id = url.substring(url.lastIndexOf('/') + 1);

if($('.post-list-item[data-post-id="'+id+'"]').length > 0){
    $('.post-list-item[data-post-id="'+id+'"]').find('.mpblog-post-edit').trigger('click');
}

Thanks

7
  • i edited my question. but if i use hide() it working but when i use trigger() it not working. Please help me
    – Boll
    Jul 10 '20 at 7:07
  • Try $('.post-list-item[data-post-id="'+id+'"]').find('.mpblog-post-edit')[0].click(); It would be helpfull if you add the html dom hierarchy showing the elements in your question for reference. Jul 10 '20 at 8:28
  • i tried but it not working
    – Boll
    Jul 10 '20 at 8:32
  • Please add your html code in your question to have better idea, also see if there is any console error. Jul 10 '20 at 8:34
  • i added for you see
    – Boll
    Jul 10 '20 at 8:43
0
 var url = window.location.href;
 var id = url.substring(url.lastIndexOf('/') + 1);

if(typeof id !== 'undefined' && $('.post-list-item[data-post-id="'+id+'"]').length > 0){
    $('.post-list-item[data-post-id="'+id+'"]').find('.mpblog-post-edit').trigger('click');
}
6
  • i edited my question. but if i use hide() it working but when i use trigger() it not working. Please help me
    – Boll
    Jul 10 '20 at 7:29
  • Why do you not use a simple click(); ? like this : $('.post-list-item[data-post-id="'+id+'"]').find('.mpblog-post-edit').click();
    – D Singh
    Jul 10 '20 at 7:44
  • i tried but it not working also
    – Boll
    Jul 10 '20 at 7:52
  • Are you getting any error?
    – D Singh
    Jul 10 '20 at 8:02
  • no don't have error
    – Boll
    Jul 10 '20 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.