6 added 1249 characters in body
source | link
5 added 2044 characters in body; edited title
source | link

How to injectexecute javascript within drupal for a specific field on a specific page?

IThe Initil question I have a input field which should activate a function on the onfocus javascript event on a field on the user page.

How to get the onfocus=... inside the input element? Is the custom code ok?

Addition to this question.

Thxs you so much for all the comments. Based on that I learned i few things more so I now understand what the real question should be.

To understand how I can executed javascript code in a certain field on a certain page in drupal.

I assumed this was needed by a callback but this is not encoured according to @Jimmy so I'm trying to understand the way it should be according to drupal standards. Also I was suprised that no change in HTML was needed as @Adrian gave an example. I could not get it run because of the typo :-( So in mean time I made another solution.

The script is basicly just a real time concatination of first, middle and last name into a fullname field.

(function ($) {
  $(document).ready(function(){

        function setFullName() {
            var nameParts = [document.getElementById("edit-field-voornaam-und-0-value").value,  
                             document.getElementById("edit-field-tussenvoegsel-und-0-value").value, 
                             document.getElementById("edit-field-achternaam-und-0-value").value],
                name = nameParts.join(" ").trim();

            document.getElementById("edit-field-naam-und-0-value").value = name;
        }

        var fields = [document.getElementById('edit-field-voornaam-und-0-value'), 
                      document.getElementById('edit-field-tussenvoegsel-und-0-value'), 
                      document.getElementById('edit-field-achternaam-und-0-value')];

        for (var i = 0; i < fields.length; i++) {
            fields[i].addEventListener("keyup", setFullName);
        }


  });
})(jQuery);

So this is working and I put it in a custom module.

I still do not understand how I should be if I rewrite the code following the example of @adrian as this code is still executed on every page and not a specific page.

Thank you for learning !! and not using the correct words. I'm not native english ...

How to inject javascript?

I have a input field which should activate a function on the onfocus javascript event on a field on the user page.

How to get the onfocus=... inside the input element? Is the custom code ok?

How to execute javascript within drupal for a specific field on a specific page?

The Initil question I have a input field which should activate a function on the onfocus javascript event on a field on the user page.

How to get the onfocus=... inside the input element? Is the custom code ok?

Addition to this question.

Thxs you so much for all the comments. Based on that I learned i few things more so I now understand what the real question should be.

To understand how I can executed javascript code in a certain field on a certain page in drupal.

I assumed this was needed by a callback but this is not encoured according to @Jimmy so I'm trying to understand the way it should be according to drupal standards. Also I was suprised that no change in HTML was needed as @Adrian gave an example. I could not get it run because of the typo :-( So in mean time I made another solution.

The script is basicly just a real time concatination of first, middle and last name into a fullname field.

(function ($) {
  $(document).ready(function(){

        function setFullName() {
            var nameParts = [document.getElementById("edit-field-voornaam-und-0-value").value,  
                             document.getElementById("edit-field-tussenvoegsel-und-0-value").value, 
                             document.getElementById("edit-field-achternaam-und-0-value").value],
                name = nameParts.join(" ").trim();

            document.getElementById("edit-field-naam-und-0-value").value = name;
        }

        var fields = [document.getElementById('edit-field-voornaam-und-0-value'), 
                      document.getElementById('edit-field-tussenvoegsel-und-0-value'), 
                      document.getElementById('edit-field-achternaam-und-0-value')];

        for (var i = 0; i < fields.length; i++) {
            fields[i].addEventListener("keyup", setFullName);
        }


  });
})(jQuery);

So this is working and I put it in a custom module.

I still do not understand how I should be if I rewrite the code following the example of @adrian as this code is still executed on every page and not a specific page.

Thank you for learning !! and not using the correct words. I'm not native english ...

4 Rollback to Revision 2
source | link

I have a input field which should activate a function on the onfocus javascript event on a field on the user page.

Step 1 Add code to a custom module:

(function ($) {
  $(document).ready(totalname(){
    document.getElementById("fullname").style.background = "yellow";
  });
})(jQuery);

The currently HTML is:

<input type="text" id="fullname"><br>

And it should become like this:

<input type="text" id="fullname" onfocus="totalname()"><br>

How to get the onfocus=... inside the input element?

For me the most important part is that I need to include more javascript code to the element (or to another elements), so I need to know the way to attach Is the jscustom code to the form in Drupal.ok?

I have a input field which should activate a function on the onfocus javascript event on a field on the user page.

Step 1 Add code to a custom module:

(function ($) {
  $(document).ready(totalname(){
    document.getElementById("fullname").style.background = "yellow";
  });
})(jQuery);

The currently HTML is:

<input type="text" id="fullname"><br>

And it should become like this:

<input type="text" id="fullname" onfocus="totalname()"><br>

How to get the onfocus=... inside the input element?

For me the most important part is that I need to include more javascript code to the element (or to another elements), so I need to know the way to attach the js code to the form in Drupal.

I have a input field which should activate a function on the onfocus javascript event on a field on the user page.

Step 1 Add code to a custom module:

(function ($) {
  $(document).ready(totalname(){
    document.getElementById("fullname").style.background = "yellow";
  });
})(jQuery);

The currently HTML is:

<input type="text" id="fullname"><br>

And it should become like this:

<input type="text" id="fullname" onfocus="totalname()"><br>

How to get the onfocus=... inside the input element? Is the custom code ok?

3 Added information provided by the OP in the answer comments.
source | link
    Post Closed as "off-topic" by Shawn Conn, Clive
2 deleted 19 characters in body
source | link
1
source | link