Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom form field within a taxonomy. However when a new term/tag has been added I need to clear some values through javascript. How can I do this?

I found the following code snippet in WordPress:

a.post(ajaxurl,a("#addtag").serialize(),function(h)
{
    a("#ajax-response").empty();

    var f=wpAjax.parseAjaxResponse(h,"ajax-response");

    if(!f||f.errors){return}

var g=b.find("select#parent").val();

if(g>0&&a("#tag-"+g).length>0){
    a(".tags #tag-"+g).after(f.responses[0].supplemental.noparents)}
else
    {a(".tags").prepend(f.responses[0].supplemental.parents)}

a(".tags .no-items").remove();

if(b.find("select#parent")){

var e=f.responses[1].supplemental;
var c="";

for(var d=0;d<f.responses[1].position;d++){c+="&nbsp;&nbsp;&nbsp;"}

b.find("select#parent option:selected").after('<option value="'+e.term_id+'">'+c+e.name+"</option>")}

// = NOTE THE FOLLOWING LINE =============
a('input[type="text"]:visible, textarea:visible',b).val("")});
// =======================================

return false})});

It clears all the text input fields that are visible, however I have my own custom field that people can select an image and after the image has been selected a hidden input field value is changed with the selected image id and the image is shown through an image object.

However, after the user presses the 'Add new category' button I like to be able to reset the hidden input field and remove the image object.

I like to know when a category has been added so I can reset all that is necessary.

share|improve this question

1 Answer 1

Could you be more specific on the "when a new term/tag has been added" part? Are you doing it yourself, as in you call the backend through admin-ajax.php and update the front-end accordingly? Including some code or a link to jsFiddle wouldn't hurt.

There's a create_term action you can hook into in the back-end. Just put this into functions.php or your plugin code:

function your_create_fn( $term_id, $tt_id, $taxonomy ){
    // do your thing here
}
add_action( 'create_term', 'your_create_fn', 10, 3 );

Alternatively, if you're doing it yourself then you obviously control the entire process, so you know exactly when you've successfully added a new term and can return something to the JS function that indicates you need to clear those values.

share|improve this answer
    
I edited my question. –  Mark Mar 11 '13 at 17:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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