6

Got the next html code:

<form name="frm">
   <textarea class="textarea" name="textarea" id="textarea">
     <?php
        foreach ($textarea as $textareashow){
          echo $textareashow."\r\n";
        }?>
   </textarea><br><br>
   <input type="button" name="callbutton" value="Push button" id="execbutton" />
</form>

And the next JavaScript code:

$(document).ready(function () {
  $('#callbutton').click(function () {
     alert("HELLO!!!");
  });
});

What I want is to call the JavaScript function from his button input name. Is it possible? and from his input id? Thank you very much.

3 Answers 3

5

Try this:

$(document).ready(function () {
    $('#execbutton').click(function () {
       alert("HELLO!!!");
   });
});

Will work.

Explanation: # is used to select the element by id and . is used to select an element by classname. you are selecting element by name so you need to use the $("input[name=callbutton]"). If you are selecting any element by its property or attribute then you need to use it in [] brackets. [property_name=property_name_value] like this.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much to all of you guys! After a while I realized that the call link to js/jquery was below the call link to my js/textareascript.js!!!!!!! that's why the script was not working. What a cock-up!
@user3321425 nice to hear from you and your hard work. My pleasure to help you. :)
5

Of course it's possible. I've posted a jsfiddle below

You can use following jQuery selectors : #id and input[name=something] ( you don't need to use both of them )

To add a click event on a button ( in your case ) you can use .click(function(){})

So :

$(function(){
     $("#your_btn_id").click(function(){   // using selector by id
          // do something
     });

     $('input[name="your_btn_name"]').click(function(){  // using selector by name
         // do something
     });   
})

You can also combine selectors :

$(function(){
     $('#your_btn_id, input[name="your_btn_name"]').click(function(){   
          // do something
     });


     // or


    $("#your_btn_id").add('input[name="your_btn_name"]').click(function(){   
          // do something
     });
})

Fiddle

1 Comment

Code and explanation in the answer is always better (JSFiddle to back up that it works) :)
5

by input name use input[name="callbutton"] by id use the id notation #

$(document).ready(function () {
  $('input[name="callbutton"],#textarea').click(function () {
    alert("HELLO!!!");    
  });
});

Comments

Your Answer

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.