Search code examples
javascriptwordpressbookmarklet

Why browser redirect whe attempting to set the value of Wordpress media image alt field?


I am trying to write a bookmarklet to populate the Wordpress image gallery manager, clicking an image pops up the sidbar with these fields,there are alt (input), legend,and description, both are textarea. my Js snippet is:

<a href="javascript:var t=document.querySelectorAll('div.media-sidebar label input');var a=t[2];var u=document.querySelectorAll('div.media-sidebar textarea');var b=u[0];var v=document.querySelectorAll('div.media-sidebar label input');var c=u[1];a.value='Hello';">Duplicate</a>

When I click the link, i am redirected to a new page, with the text i want to set. How to do it correctly, i don't know why i am being redirected. thanks.


Solution

  • Add this.stop; in the last of your href And also use a.innerHTML because the variable a will be holding the div element which doesn't support value attribute/property.

    <a href="javascript:var t=document.querySelectorAll('div.media-sidebar label input');var a=t[2];var u=document.querySelectorAll('div.media-sidebar textarea');var b=u[0];var v=document.querySelectorAll('div.media-sidebar label input');var c=u[1];a.innerHTML='Hello';this.stop;">Duplicate</a>
    

    This will stop the default behaviour of href and exit from redirecting to the new document.