Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
92
rated 0 times [  96] [ 4]  / answers: 1 / hits: 58827  / 12 Years ago, fri, may 18, 2012, 12:00:00

I have a button on my webpage which when onClick I want it to show another div. However when I click the button it will flash in the url that it went to the correct div but then it will immediately redirect back to the main div, #home.



Code for button:



<input type=button class=flip value=Redirect To Div onClick=window.location='#targetDiv' />


When I click the the button the url will flash:



www.XXXXX.com/#targetDiv



Then go immediately to www.XXXXX.com/#home



Any ideas why it won't display the div?



Thanks


More From » html

 Answers
30

looks like your form has



       action=


or



       action=#home


instead of onclick call a function



      onclick=mylinkfunction()


...



     <script type-text/javascript>

function mylinkfunction(e) {

window.location.href=#targetDiv;

/* need to stop the form sending of the form

UPDATE as comment: This may not be exactly correct syntax
for stopping a form , look up preventing form submission */

e.preventDefault();
e.stopPropagation();

}

</script>


or if your form is irrelevent set it's action and remove the javascript allogether



     <form action=#targetDiv method=get>
<input type=submit value=go/>
</form>


but then all you would need is actually this ..



    <a href=#targetDiv>click me</a>


if by go to another div you mean jump and scroll the page to a new location it is actually an anchor you need to go to



        <a name=targetDiv/>

[#85488] Thursday, May 17, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
austonjuancarlosb

Total Points: 238
Total Questions: 89
Total Answers: 99

Location: Chad
Member since Mon, Dec 5, 2022
1 Year ago
;