• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

forms: simple reference to value of input field from another

 
Roger F. Gay
Ranch Hand
Posts: 408
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Roger F. Gay
Ranch Hand
Posts: 408
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 408
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 408
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad it's sorted out.

I still recommend considering Unobtrusive JavaScript.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic