0

I am working on an asp.net application where I have button written like this:-

<button onclick='javascript:imagech(this);' class='close_product color_dark tr_hover'><i class='fa fa-times'></i></button>

I have also a client-side javascript function for the above button:

<script type="text/javascript">
 function imagech() {
 alert('image clicked.');
 }
</script>

What I want that is whenever I click that function then the button click event should not fire page-load. The above object is not server control but it's event occurs page-load. I can also use another html control like this:

<input type='button' onclick='javascript:cartclicked(this);' value='X' class='close_product color_dark tr_hover' />

and this does not occurs page-load but I want icon also used as :

<i class='fa fa-times'></i>

inside <button> tag. but the <i> tag is not used in <input> tag. How I can <button> tag event without any page-load? Please help me someone here.

Mr Lister
46.7k15 gold badges115 silver badges156 bronze badges
asked Mar 29, 2016 at 10:12
7
  • without any page-load i am not sure if i understand this one correctly Commented Mar 29, 2016 at 10:14
  • Use JavaScript event binding inside window.onload handler Commented Mar 29, 2016 at 10:14
  • @Rayon Dabre how can i use that? Commented Mar 29, 2016 at 10:15
  • window.onload= function(){ document.getElementById(YOUR_ID).addEventListener('click', ....) } Commented Mar 29, 2016 at 10:16
  • @guradio when i use <button> tag which is a html control not a server control and when i click on this the page-load occurs which i want to stop. Commented Mar 29, 2016 at 10:16

2 Answers 2

2

it seems you are using <button> inside a <form> element. By default a button acts as a submit button and it tries to submit your form on click.

In order to continue using button and avoid this issue you need to specify type as button as shown below

<button type="button" onclick='javascript:imagech(this);' class='close_product color_dark tr_hover'><i class='fa fa-times'></i></button>
answered Mar 29, 2016 at 10:37
Sign up to request clarification or add additional context in comments.

Comments

2

If I understand you correctly the problem is that the button is inside a form (The main asp.net form) and the click event post the form to the server. That's why you the page load occurred.

The solution Just add to the button (it doesn't matter if you are using input or button) attribute type="button".

answered Mar 29, 2016 at 10:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.