1

Hi I was just testing jQuery with React JS for some reason. But facing an issue like this. when I try this in console

$(".cities-container select").val()

I got this

Uncaught TypeError: $(...).val is not a function

But when I try this

$(".cities-container select")

I got whole select element in the console, means jQuery is working but not .val method

any suggestions...?

7
  • When you did $(".cities-container select"), what was the output? Commented Jun 22, 2017 at 9:20
  • 1
    $ is a shortcut for the document query selector in the browser. developer.mozilla.org/en-US/docs/Web/API/Document/querySelector Commented Jun 22, 2017 at 9:22
  • I got Whole HTML of select element Commented Jun 22, 2017 at 9:23
  • @h_a86 Can you paste the output here? Commented Jun 22, 2017 at 9:25
  • <select> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="opel">Opel</option> <option value="audi">Audi</option> </select> Commented Jun 22, 2017 at 9:26

3 Answers 3

2

Try this:-

$(".cities-container select")[0].value

This should resolve your problem.

Sign up to request clarification or add additional context in comments.

7 Comments

This is what you tried right $(".cities-container select")[0].value?
Yes I used cities
@h_a86 what is the console.log for this $(".cities-container select")[0]
down voting the answer is so wrong here.We are trying to help.
I didn't, believe me
|
1

jQuery selectors often return an array of elements. Try getting the first element, like so:

$(".cities-container select")[0].val()

Or using the .get() method:

$(".cities-container select").get(0).val()

Comments

0

You get $ as an alias for document.querySelector when jQuery isn't active. This means using $(".cities-container select").value instead of jQuery's $(".cities-container select").val().

Check that the jQuery library is loaded by checking that jQuery version isn't undefined (see snippet for an example) or by trying to make a new jQuery instance in the console using the jQuery function.

References: https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector https://developers.google.com/web/tools/chrome-devtools/console/command-line-reference

$(document).ready(
  function() {
    var selectOpt = $("#select-opt").val();

    console.log(selectOpt);
  }
);

var jQueryNotActivated = $().jquery == undefined; // jquery alias isn't active

if(jQueryNotActivated) {
  // In chrome, $ is alias for document.querySelector
  var selectOptwithdQS = $("#select-opt");
  console.log(selectOptwithdQS.value);  
}
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">  </script>
</head>


<body>
  <select id="select-opt">
    <option val="bus">Bus</option>
    <option val="box">Box</option>
  </select>
</body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.