0

I am on an editing post page on wordpress and have this option to create a target link:

https://www.vtxfactory.org/images/11111.jpg

The default option is "None", I want it to be the "Custom Link..." one, for that, I have the following info:

<select name="imt_team_href" onchange="imtTeamCustomHref(this.value, '#imt_custom_href_div');">
<option value="-2">None</option>
<option value="-1">Custom Link ...</option>
<option value="2">Sample Page</option>

...

I know I could edit the file and add a "selected" code, but I can't find the file that needs to be edited.

I've tried but without success:

$(function() {
    $("#imt_custom_href_div").val("-1");
});

How can I, using javascript or whatever other method, change the predefined value?

Thank you.

Rui
  • 39
  • 7
  • Possible duplicate of [how to set default value for HTML select?](https://stackoverflow.com/questions/19611557/how-to-set-default-value-for-html-select) – Obsidian Age Jul 09 '17 at 23:40
  • The solution provided on that post does not help. – Rui Jul 09 '17 at 23:46
  • That's because your ` – Obsidian Age Jul 09 '17 at 23:52
  • Yeah I know it doesnt have ID, but I don't have a way to assign one to it. Thanks, gonna check those out. – Rui Jul 10 '17 at 00:30

1 Answers1

0

The selector is wrong, it is looking for an ID but the <select> shown has no ID

Try

$('select[name="imt_team_href"]').val("-1");
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • Works fine here: https://jsfiddle.net/6zknnrow/ Perhaps that element doesn't exist when page loads or there is other code affecting it – charlietfl Jul 09 '17 at 23:52
  • Hey, wordpress has some problems with javascript, so I had to change the code to this jQuery.noConflict(); jQuery(function() { jQuery('select[name="imt_team_href"]').val("-1"); }); And now its working, thanks :) – Rui Jul 09 '17 at 23:54
  • Ah yes...can avoid that problem by wrapping your jQuery code in `(function($){ /* code can use $ here*/})(jQuery)` – charlietfl Jul 09 '17 at 23:55
  • Is there a way to add the "selected" parameter to the "Custom Link ..." value? – Rui Jul 10 '17 at 00:04
  • Assigning the value will automatically set the `selected` property on that ` – charlietfl Jul 10 '17 at 00:16
  • He did change the value to Custom Link but it appears that it didn't stay active, I'll try the code you provided now and I'll tell you something. Should I add that code next to the previous one? – Rui Jul 10 '17 at 00:29
  • It returns -1 as a message – Rui Jul 10 '17 at 03:35