On click of a button, firing a JS function and that too is..

Post Reply
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

On click of a button, firing a JS function and that too is..

Post by Neofriend » Mon May 24, 2010 12:47 am

Hey everyone... how are you my friend Neo :)

A little tricky situation here, which is bugging my head - at a glance, brain says no its not possible. But heart says It is very possible, maybe in a different way. Need your help.

The situation is, there is one text box for entering any text, when the text is entered, you can click an ASP.NET button. Which then fires a onclick event, and that basically sets the text of textbox in to a variable. Then since the page refreshes, the variable is then called through javascript. So on first website visit, the function is called but the no value is passed as nothing is written in textbox. Then on writing into textbox and click the button calls the function and does the task.

Now, what is the situation that needs solving? I need to have many of such textboxes and many of such buttons. Each button for each textbox. And for all the buttons, there will be specific functions. Suppose, we have two textbox and two buttons and two js funtions.

Now if I enter some text into textbox1 and hit searchButton1. it works, as i've also loaded the javascript function on page load. But when I enter something on textbox2, and hit search button2, I want a specific function to load. And again, the value in textbox2 should also be passed to javascript before javascript function fires off.

I know its possible, this way or the other... its a bit tricky though.

Thanks for the intention to help! :)
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: On click of a button, firing a JS function and that too is..

Post by Neo » Tue May 25, 2010 8:27 pm

I'm fine dear friend...hope you are doing fine too....

For this, you can dynamically load the js file based on the user input.

Code: Select all

function load_js_file(filename){

     var jsfile = document.createElement('script');
     jsfile.setAttribute("type","text/javascript");
     jsfile.setAttribute("src", filename);
     document.getElementsByTagName("head")[0].appendChild(jsfile);
}
 
Now you can write you code to select the js file you need to load for each button (may be with the same function name if you want).

Code: Select all

     if (button1){
          load_js_file("btn1.js");
     }
     else if (button2){
          load_js_file("btn2.js");
     }
     ..........
     ..........
 
Are we talking about the same subject ;)
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: On click of a button, firing a JS function and that too is..

Post by Neofriend » Tue Jun 01, 2010 6:10 pm

Hey dear,

I just noticed this response, i was actually waiting to receive the updates via email. As previously, whenever anyone responded to the forum thread, an email was received.

But I don't received emails anymore, not sure why.

In the mean time, i've implemented it in a different way. Separate page for each JS function.

That solution is a genious though :)

Take care.
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: On click of a button, firing a JS function and that too is..

Post by Neofriend » Tue Jun 01, 2010 6:22 pm

My friend, you know my cell stopped working. Tried everywhere to get it repaired. That is almost complete though - Will be finalizing things and presenting that. But now am doing a simple yet different project. Hint: It relates to something you and everyone else using internet does everyday.

Not that simple though, am trying my best, hopefully you will like it :)

Won't disappoint you my teacher! ;)
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: On click of a button, firing a JS function and that too is..

Post by Neo » Tue Jun 01, 2010 7:32 pm

Good luck :)
Post Reply

Return to “ASP & ASP.Net”