Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2014
    Thanked 0 Times in 0 Posts

    handling returned value(onclick action of anchor tag ) when html is in string format


    I have an anhor tag which is in string format that is str=str+ '<a onclick=\'myFunction("'send'")\' href="#">hello</a>';
    now on click it goes to myFunction("send"), now I want myFunction to return a value ,and based on returned value i want certain action to be carried,
    return Value;
    function mainFunction()
    str=str+ '<a onclick=\'myFunction("'send'")\' href="#">hello</a>';
    //handling the returned value of onclick
    if(Value == "correct")
    { // do something }
    { //other action }


    I will be grateful if someone can provide a working example of the above scenario which I want to implement.

  2. #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Sydney, Australia
    Thanked 932 Times in 919 Posts
    Why are you using an <a> tag with a placeholder href? If you don't want the href to go somewhere when JavaScript is disabled then use <button> instead of <a> as apart from the href they both function the same (and using CSS can be styled to look the same).

    Stop jumbling your HTML and JavaScript together and the code will be easier to write and maintain. It is particularly ugly when you have JavaScript buried inside HTML buried inside JavaScript as you have done.

    The way you have the function attached you would need to add all of that subsequent processing inside of the onclick in the HTML.

    Start with clean HTML - say <div id="myaction"></div>

    Then your JavaScript can add the code into the div without getting the HTML and JavaScript confused.

    b = createElement('button');
    b.onclick = function() {
       action = myFunction('send');
       if ('correct') === action) {
         // do something 
       }  else  { 
         //other action 
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts