2

in my html website I can call any javascript function through anchor like this

<a href="javascript:function();"></a>

but when I try to add this in wordpress menu option then after saving menu , everything vanished from there and field becomes empty. how can I call functions through wordpress Menu's.

any one can help please ?

1 Answer 1

1

It's hard to say what the problem is without seeing your JS, but you could try this:

<ul>
    <li><a id="red" href="#" onclick="myFunction()">List Item with Function</a></li>
    <li><a href="#">List Item without Function</a></li>
</ul>

function myFunction(){
    document.getElementById('red').setAttribute('style', 'color: red');
}

Here's a fiddle: http://jsfiddle.net/BSshG/1/

Or handle the click even with JavaScript and don't worry about changing the HTML:

document.getElementById('red').onclick = function(){
    this.style.color = "red";
}

Another fiddle: http://jsfiddle.net/BSshG/2/

Sign up to request clarification or add additional context in comments.

4 Comments

hey I donot have any issues in javascript , I just want to call those functions through wordpress own menu's that we created through GUI
Try using the onclick attribute on your a element to call your function, as I have done in my answer. If that doesn't work, the issue could be one either with Wordpress not loading your script (do a search for enqueueing scripts), or possibly with the way Wordpress is generating the HTML (for instance, you probably do not want to put the HTML in the WYSIWYG editor, but in the actual source).
how can I add onclick event there while there's only URL field in wordpress . you are not getting my point
Updated my answer with another option. If you don't know what the id of the a is, you can view the site in the browser, right-click on that element and choose "Inspect Element".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.