最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

images - Access html element in media modal with jQuery

programmeradmin0浏览0评论

I'm using the following code to add the required attribute to a textarea tag.
HTML:

<label class="setting" data-setting="caption">
        <span class="name">Beschriftung</span>
        <textarea></textarea>
</label>

JavaScript:

jQuery(function(){
       jQuery("label[data-setting='caption']").find("textarea").prop('required',true);
});

/

When I add an image to a text field in a post wordpress opens the media modal.

When I inspect the media modal with right click -> Inspect I can navigate to the following HTML element.

<label class="setting" data-setting="caption">
        <span class="name">Beschriftung</span>
        <textarea></textarea>
</label>

How can I make my JavaScript code run the jQuery function on the media modal?

I'm using the following code to add the required attribute to a textarea tag.
HTML:

<label class="setting" data-setting="caption">
        <span class="name">Beschriftung</span>
        <textarea></textarea>
</label>

JavaScript:

jQuery(function(){
       jQuery("label[data-setting='caption']").find("textarea").prop('required',true);
});

https://jsfiddle/qp30Ljvo/

When I add an image to a text field in a post wordpress opens the media modal.

When I inspect the media modal with right click -> Inspect I can navigate to the following HTML element.

<label class="setting" data-setting="caption">
        <span class="name">Beschriftung</span>
        <textarea></textarea>
</label>

How can I make my JavaScript code run the jQuery function on the media modal?

Share Improve this question edited Aug 28, 2019 at 15:58 Osvald Laurits asked Aug 28, 2019 at 11:17 Osvald LauritsOsvald Laurits 1337 bronze badges 2
  • 1 Can you explain what you're trying to do? Should you be setting the caption somewhere in a Gutenberg object model rather than directly on the page? – Rup Commented Aug 28, 2019 at 11:38
  • I'm trying to change the element <textarea></textarea> to <textarea required></textarea>. I know the jQuery syntax. I don't know how to access the page which contains the html element. – Osvald Laurits Commented Aug 28, 2019 at 11:42
Add a comment  | 

1 Answer 1

Reset to default 0
(function($){
//since only one label with data-setting='caption' exists, use this selector
    $("[data-setting=caption]").children('textarea').attr('required', true);
    })(jQuery);

I'm more curious as to how adding this would affect anything since these labels are not inside a <form> element?

发布评论

评论列表(0)

  1. 暂无评论