Вопрос

How can we perform onclick event on href, I wrote the following code but ot does not perform any action. What's wrong with my code ?

<a href="javascript:sendCategoryDetails()" id="maillocation" data-mini="true" data-role="button" data-theme="b" data-inline="true">Send Category Details</a>

My Java script code is as follows,

function sendCategoryDetails()
      {
         alert("control is here");
      }
Это было полезно?

Решение

html

<a href="javascript:void();" onClick="sendCategoryDetails();" id="maillocation" data-mini="true" data-role="button" data-theme="b" data-inline="true">Send Category Details</a>

fiddle

Другие советы

use

<a href="#" id="maillocation" data-mini="true" data-role="button" data-theme="b" data-inline="true" onClick="sendCategoryDetails()">Send Category Details</a>

javascript: has been disabled for security reasons in new versions of most browsers or its disabled by defualt

try this Send Category Details

    <script>
 function sendCategoryDetails()
 {
   window.location.href("URL");
  }
  </script>

Please replace your anchor html to this

<a href="javascript:void(0)" onClick="sendCategoryDetails()" id="maillocation" data-mini="true" data-role="button" data-theme="b" data-inline="true">Send Category Details</a>

OR using jQuery

<a href="javascript:void(0)"  id="maillocation" data-mini="true" data-role="button" data-theme="b" data-inline="true">Send Category Details</a>

<script>
$(document).ready(function(){
  $('#maillocation').click(function(){
     sendCategoryDetails()
  });
});
</script>

href attribute is used to add link.You cannot call the function on href

<a href="javascript:void();" id="maillocation" data-mini="true" data-role="button" data-theme="b" data-inline="true" onClick="sendCategoryDetails()">Send Category Details</a>

href="#" will post the same page.It adds # in your url not good

href="javascript:void();" use this to avoid

Use javascript:document.location.href=function_name();

Check this Demo Fiddle

<script type="text/javascript">
      function sendCategoryDetails(){
         alert("control is here");
      }
</script>

<a href="javascript:document.location.href=sendCategoryDetails();" 
   id="maillocation" 
   data-mini="true" data-role="button" 
   data-theme="b" data-inline="true">Send Category Details</a>

100% worked, dont know why minus vote!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top