permaculture playing cards*
The moose likes HTML, CSS and JavaScript and the fly likes forms: simple reference to value of input field from another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "forms: simple reference to value of input field from another" Watch "forms: simple reference to value of input field from another" New topic
Author

forms: simple reference to value of input field from another

Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 397
This is driving me nuts! I know I did something like this years ago, but can't make it work now.

I have a basic form with a text input and button. When I click the button, I want onclick to call a javascript function and pass the input text string to it. I've tried various combinations. The only one actually passing "value" is the one I'm posting here. The catch is that it always passes the string "test". It doesn't matter what I put in the input text field. I've tried not defining value and using value="" and get nothing. I've even tried to set value=eval(this.value)



Correlation does not prove causality.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61645
    
  67

With jQuery, you'd use $('#productItem').val() to get the value. But then, using jQuery you wouldn't be setting up the handler up like this.

Without jQuery, you'd use document.getElementById('productItem').value


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 397
Bear Bibeault wrote:With jQuery, you'd use $('#productItem').val() to get the value. But then, using jQuery you wouldn't be setting up the handler up like this.

Without jQuery, you'd use document.getElementById('productItem').value


No, that's just a different way of getting the node. In this context. productItem is the same as document.getElementById('productItem'). You don't really need the latter unless you're outside the form context.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61645
    
  67

Roger F. Gay wrote:that's just a different way of getting the node

I'd be using unobtrusive JavaScript in any case, so my code would not be embedded in the HTML. In any case, my code fetches the correct run-time value.
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 397
Bear Bibeault wrote:
Roger F. Gay wrote:that's just a different way of getting the node

I'd be using unobtrusive JavaScript in any case, so my code would not be embedded in the HTML. In any case, my code fetches the correct run-time value.


I don't know what your response has to do with jQuery. document.getElementById("myId").value is straightforward DOM stuff like I do every day in JavaScript. I tried that, both within the call and inside the function that's called (i.e. function call with no variable). Still didn't work. I had some trouble earlier related to the list parameter. I'm starting to wonder whether that's the problem here. I know the function is being called. And once there, it seems perfectly obvious that the form document.getElementById("myId").value should get the job done.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61645
    
  67

Roger F. Gay wrote:
Bear Bibeault wrote:
Roger F. Gay wrote:that's just a different way of getting the node

I'd be using unobtrusive JavaScript in any case, so my code would not be embedded in the HTML. In any case, my code fetches the correct run-time value.


I don't know what your response has to do with jQuery.

It doesn't. jQuery is mentioned nowhere in the response you quoted. Unobtrusive JavaScript is recommended regardless of whether jQuery is used or not. It sort of like CSS -- it's preferred to avoid the use of style attributes embedded in the HTML.

I tried that, both within the call and inside the function that's called (i.e. function call with no variable). Still didn't work.

Please post an SSCCE with your changes. I tried it in your example and it worked fine for me.
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 397
I'm going to apologize for trying to get that one last thing done late at night all the time. Turns out this one has been something done like that step by step from the beginning ... just that little extra effort thing. Also makes me a little bit of a tick sometimes when I post probable. (Don't mean to be.)

You're absolutely right. I put the little thing in it's own html file all by its lonesome and it worked perfectly. No problem. Then I realized that a week ago (late at night), I'd dynamically copied that form to another place on the page resulting in duplicate ids.

Nobody's perfect.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61645
    
  67

Glad it's sorted out.

I still recommend considering Unobtrusive JavaScript.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: forms: simple reference to value of input field from another