Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using Jquery Input Element in Javascript Function

Posted on 2017-08-14
5
Medium Priority
?
44 Views
Last Modified: 2017-08-19
I have a form that posts via javascript / jquery / ajax

I am trying to validate some fields server side before actual submission I was doing this to set the variable

myVar=$('#myVarFormID').val();

Open in new window


when I check the console, my variable is set appropriately and shows as intended, but when I go like this after setting it
nextFunction(myVar);

Open in new window


The function thinks the variable is [htmlInputElement]

I dont understand why the output shows fine but the script function wont take it in.  Is there something extra I need to do to pass this form element into a different function?
0
Comment
Question by:weekapaug
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 59

Expert Comment

by:HainKurt
ID: 42254757
need full code, or jsfiddle.net demo (or html from browser)

what is this by the way? #myVarFormID
0
 

Accepted Solution

by:
weekapaug earned 0 total points
ID: 42254766
I already figured it out. I suspected I couldnt mix jQuery with javascript so when I set the variable with

document.getElementById('myVarFormID').value instead of the previous jquery code it worked
0
 
LVL 59

Expert Comment

by:Julian Hansen
ID: 42254992
I suspected I couldnt mix jQuery with javascript so when I set the variable with

document.getElementById('myVarFormID').value instead of the previous jquery code it worked
This does not make sense - jQuery is JavaScript - anything you set with one is available in the other.

To access the underlying Dom element from a jQuery object all you need to do is provide an index
var element = $('#someid');

Open in new window

Then element[0] is the same as document.getElementById('someid')
0
 

Author Closing Comment

by:weekapaug
ID: 42261456
figured it out
0
 
LVL 43

Expert Comment

by:Rob
ID: 42261903
Further to Julian's comment, it should be noted that there is an error in the asker's code (that's not posted) as the code in the question does work when taken on its own.  In other words, it is wrong to assume that document.getElementById('myVarFormID').value will solve the issue.

https://jsbin.com/poqiteh/edit?html,js,console

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>JS Bin</title>
    <script src="https://code.jquery.com/jquery-3.1.0.js"></script>

  </head>
  <body>
    <form action="">
      <input type="hidden" value="somevalue" id="myVarFormID"/>
    </form>
  </body>
</html>

Open in new window


function test(myval) {
  console.log(myval);
}

$(function() {
  var myval = $('#myVarFormID').val();
  test(myval);
})

Open in new window


Outputs:
somevalue

Open in new window

0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question