/    Sign up×
Community /Pin to ProfileBookmark

jquery onclick do something – the page moves on top why??????????

the problem is that when i click a href and target a jquery function the code executed normally but the page moves on top always….
the javascript code:

[code=php]
<script type=”text/javascript”>
$(document).ready(function(){
$(‘.wallmore’).click(function (e){
var lastid = $(this).attr(“id”);
last_msg_funtion(lastid);
});
function last_msg_funtion(lastid){
$.ajax({
type: “POST”,
url: “<?php echo $root;?>includes/load.php”,
data: ‘lastid=’+lastid,
dataType: “html”,
success: function(msg){
$(msg).insertBefore(‘#last_msg_loader’);
}
});
}
});
</script>
[/code]

and the load.php is:

[code=php]
<?php
include_once “../myconnect.php”;

function loadmember ($gallery, $isdefault, $img_type){
$root = “http://”.$_SERVER[‘HTTP_HOST’].”/”;
if($isdefault==’yes’){
$default=mysql_fetch_array(mysql_query(“select * from sbrecipe_def_image where sbdef_image_id=”.$gallery.””));
return “”.$root.”uploadimages/default/”.$default[“cat_image”].””;
}
elseif($isdefault==’no’){
$query_images = mysql_fetch_array(mysql_query(“select * from sbrecipe_images where sbimages_id=”.$gallery.””));
switch ($img_type){
case “0”:
return $root.”uploadimages/”.$query_images[“sb_image_url”];
break;
case “1”:
return $root.”uploadimages/”.$query_images[“sb_thumb”];
break;
case “2”:
return $root.”uploadimages/”.$query_images[“sb_thumb2”];
break;
case “3”:
return $root.”uploadimages/”.$query_images[“sb_thumb3”];
break;
}
}else{
}
}
?>
<?php
if($_POST[‘lastid’]){
$last_msg_id=$_POST[‘lastid’];
$sql=mysql_query(“SELECT * FROM comments WHERE id < ‘$last_msg_id’ ORDER BY id DESC LIMIT 1″);
$last_msg_id=””;

while($row=mysql_fetch_array($sql)){
$user=mysql_fetch_array(mysql_query(“select * from sbrecipe_members where sb_id=”.$row[‘user’].””));
$msgID= $row[‘id’];
?>
<div class=”comment” id=”<?php echo $msgID; ?>”>
<div class=”avatar”>
<img src=”<?php echo loadmember( $user[“sb_member_image”], $user[“sb_member_isdefault”], ‘2’); ?>” />
</div>

<div class=”name”><?php echo $user[“sb_username”]; ?></div>
<div class=”date” title=”Added at <?php echo date(‘H:i on d M Y’,$row[‘dt’]); ?>”><?php echo date(‘d M Y’,$row[‘dt’]); ?></div>
<p><?php echo $row[‘body’]; ?></p>
</div>
<?php
}
}
?>
<script type=”text/javascript”>
$(document).ready(function(){
jQuery(‘.wallmore’).attr(“id”, “<?php echo $msgID; ?>”);
});
</script>
[/code]

is anyboby tell me why this is happening and give me a solution???…thanks in advance….

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@KorOct 29.2010 — The explanation is simple. You probably want to attach an onclick event to a link which has an empty href or more probably a href="#".

The solve is as simple. You need to return false the action/event, otherwise "#" will automatically be interpreted as "#top".

I am not a fan of JQuery, but I suppose you could try this:
<i>
</i>$(document).ready(function(){
$('.wallmore').click(function (e){
var lastid = $(this).attr("id");
last_msg_funtion(lastid);
[B][COLOR="Blue"]return false[/COLOR][/B]
});
×

Success!

Help @dpsim spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...