dcsimg
www.webdeveloper.com
Results 1 to 8 of 8

Thread: Basic question about attaching a function to ontouchstart though generated elements.

  1. #1
    Join Date
    Jan 2017
    Posts
    5

    Basic question about attaching a function to ontouchstart though generated elements.

    Hey guys, I am new to JS and I am looking for some help on this,

    My project has to generate the pages entirely through JS, essentially what I am looking to do is recreate this; in JS:

    Code:
                 
    <input type="text" name="userName" id="userName"/>
    <button class="button" ontouchstart="window.app.enterField()" >Enter</button>
    What I am having an issue with is calling 'windows.app.enterField()'
    once the generated button is pressed.
    This is my current progress:

    Code:
        
        var ControllerScreen = document.getElementById("ControllerScreen");
    
        function InputField() {
            var newPara = document.createElement("p");
            var input = newPara.appendChild(document.createElement("input"));
            input.name = "userName";
            input.id = "userName";
    
            var button = newPara.appendChild(document.createElement("Button"));
            button.ontouchstart = "window.app.enterField";
            button.className = "Button";
            button.textContent = "First Button";
    
            document.body.insertBefore(newPara, ControllerScreen);
        }
    Can I attach "window.app.enterField()" to a generated button, on "ontouchstart"? I can't seem to get it working...

    Also one last question, when I am referencing the input field 'later on in my code' with var text = userName.value; am I getting the ID or the name?

    Thanks guys!

  2. #2
    Join Date
    Jan 2017
    Posts
    5
    Also, I need to use ontouchstart, OnClick will not work for what I am doing.

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,256
    Question is what happens when you have a user who has no javascript enabled or has a device that reads HTML but not javascript?
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  4. #4
    Join Date
    Jan 2017
    Posts
    5
    Quote Originally Posted by \\.\ View Post
    Question is what happens when you have a user who has no javascript enabled or has a device that reads HTML but not javascript?
    That's not a concern to me right now Just need to get this function working.

  5. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,256
    It should be a concern, many people disable JavaScript on their systems and devices and in my case I have 4 mobiles myself that run HTML but not JavaScript, one will run Java but thats also limited on what is possible.

    Your coding should be built around a `degrading gracefully` attitude rather than `not my problem` stand point.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  6. #6
    Join Date
    Jan 2017
    Posts
    5
    Quote Originally Posted by \\.\ View Post
    It should be a concern, many people disable JavaScript on their systems and devices and in my case I have 4 mobiles myself that run HTML but not JavaScript, one will run Java but thats also limited on what is possible.

    Your coding should be built around a `degrading gracefully` attitude rather than `not my problem` stand point.
    Ill be honest with you, that has no concern to my project. I appreciate you helping out
    with best practices, but I really do need to just set it up... Would you mind helping me with the original question?

    Thanks!

  7. #7
    Join Date
    Jan 2017
    Posts
    5
    This question is still open, could we stick to the original question without being derailed.

    Code:
    var ontouchstart = element.getAttribute("ontouchstart");
    if (ontouchstart) {
         element.setAttribute("onmousedown", ontouchstart);
    }

  8. #8
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,256
    what is element? How have you initialized that object?

    Is it like this? http://www.w3schools.com/jsref/met_e...tattribute.asp / https://developer.mozilla.org/en/doc...t/getAttribute
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles