Datsik
Datsik

Reputation: 14824

How do I get the content of an input text?

I'm simply trying to use whatever is typed into the <input type="text"> as a variable for my script.

I've tried .select() .value() .val() and it keeps returning undefined or null. I'm trying to do it async so the page doesnt have to refresh but it doesn't want to work as I intend.

$(function() {
    $('#changeDistance').click(function(e) { e.preventDefault();
        console.log('Triggered');
        setDistance = $('#setDistance').select();
        console.log($('#setDistance').select());
        for (var i = 0; i < markersArray.length; i++) {
            markersArray[i].setMap(null);
        }
        for (var i = 0; i < database.length; i++) {
            createMarker(database[i].latitude, database[i].longitude, database[i].markerTitle, initialLocation);
        }

    });
});

is where I'm trying to use it

Upvotes: 0

Views: 121

Answers (4)

asifsid88
asifsid88

Reputation: 4701

First you need to specify and Id to your input tag

Use this

$('#inputID').val()` 

With ID you need to use # and if you specify a class then you need to use period .
If you give class (class is assigned to many attribute) then you will get array. So you need to access each element by index

$('.classname').each(function() {  
   $(this).val();  
});

Upvotes: 2

Prabhakaran Parthipan
Prabhakaran Parthipan

Reputation: 4273

you can use by Textbox id

$("#TextboxId").val();

or

$("input[type='text']").val();

or

$(".text-classname").val();

Upvotes: 0

user2063626
user2063626

Reputation:

Try

$("input[id$='setDistance']").val();

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

Assuming your input is indeed as follows: <input type="text" id="setDistance" /> then the correct function to get its value in jQuery is .val().

However, if you write $("#setDistance").val() then I will lynch you. Use document.getElementById('setDistance').value instead.

Upvotes: 4

Related Questions