I want onclick to trigger when clicked on HTML heading as shown in code below.
<h2 class="title" onclick="viewPost('<?php echo $pageLink; ?>', '<?php echo $tabLink; ?>');">
<?php echo $menuItem["ContentMenu"]['title']; ?>
</h2>
My javascript function is as follows
<script>
function viewPost(pageLink,tabLink){
//do something
}
</script>
My problem is onclick works fine when it is in HTML link
<a href="#foo" onclick="viewPost('<?php echo $pageLink; ?>', '<?php echo $tabLink; ?>');" >
but it does not work in HTML heading. Please help.
I want onclick to trigger when clicked on HTML heading as shown in code below.
<h2 class="title" onclick="viewPost('<?php echo $pageLink; ?>', '<?php echo $tabLink; ?>');">
<?php echo $menuItem["ContentMenu"]['title']; ?>
</h2>
My javascript function is as follows
<script>
function viewPost(pageLink,tabLink){
//do something
}
</script>
My problem is onclick works fine when it is in HTML link
<a href="#foo" onclick="viewPost('<?php echo $pageLink; ?>', '<?php echo $tabLink; ?>');" >
but it does not work in HTML heading. Please help.
Share Improve this question asked Sep 27, 2013 at 3:27 nepalipunknepalipunk 7546 silver badges12 bronze badges 5- 1 Could you show the source rendered from that code? – plalx Commented Sep 27, 2013 at 3:28
- <div id="online_subscriptions"> <h2 class="title" onclick="viewPost('/ContentPages/view/1', '#online_subscriptions');">Online Subscriptions</h2> </div> – nepalipunk Commented Sep 27, 2013 at 3:31
-
@nepalipunk - Why not use the Javascript debugger to check that
viewPost
is being called - I.e. place a breakpoint at the start of that function – Ed Heal Commented Sep 27, 2013 at 3:34 - 2 @nepalipunk As far as I can tell, the source seems valid. Is there any error in the console? Can you validate that the function isin't getting called? – plalx Commented Sep 27, 2013 at 3:38
- No error or warning in console. I found that function is not being called. – nepalipunk Commented Sep 27, 2013 at 3:58
3 Answers
Reset to default 5Have you checked that the generated HTML/Javascript is valid. This works fine for me
<h1 onclick="alert('Hello');">A heading</h1>
<p>Some text</p>
See http://jsfiddle/2S56N/ for a demo
Better use jquery than javascript, Can I have onClick() event for <h1> to <h6> tags?
$('h2').click(function(){
//do something on click
});
and also I tried your code with some modifications(below) and it works fine
<script>
function viewPost(pageLink,tabLink){
alert(pageLink);
}
</script>
<?php
$tabLink = 1;
$pageLink = 2;
?>
<h2 class="title" onclick="viewPost('<?php echo $pageLink; ?>', '<?php echo $tabLink; ?>');">click</h2>
Try This I test it and its work fine:
<script>
function viewPost(pageLink,tabLink){
alert("pageLink is" + pageLink +"tabLink is "+tabLink);
}
</script>
<h2 class="title" onclick="viewPost('<?php echo 'testLink'; ?>', '<?php echo 'tabLink'; ?>');">
Click me
</h2>
<a href="#foo" onclick="viewPost('<?php echo 'testLink'; ?>', '<?php echo 'tabLink'; ?>');" > Click me</a>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745277337a4620115.html
评论列表(0条)