crusarovid
crusarovid

Reputation: 541

Using Javascript to get value from input

my html code:

<li id='category-10'>
<label class="selectit">
<input value="10" type="checkbox" name="post_category[]" id="in-category-10" /> Developmental
</label>
</li>

my javascript code:

function onCategoryClick() {
 $(".selectit").click(function(e){
    var categoryValue = $(e.target).text();
    alert("The value is " + categoryValue);
 });
}

This returns me the string "The value is Developmental" but what I want is "The value is 10" I'm new to Javascript, how would I go about targeting the value field in the input, instead of the text?

I've also tried

var categoryValue = $(e.target > input).value;

But this doesn't return anything.

Upvotes: 1

Views: 927

Answers (1)

Sushanth --
Sushanth --

Reputation: 55750

Instead of

$(e.target).text();

try

$(this).find('#in-category-10').val();

Here e.target corresponds to the label . And it has text 'Developmental' and also a nested input element. So you need to use find to access it. (If there is no id)

But ID is supposed to be unique on the page. So to access the element you can directly used the id selector

var categoryValue = $('#in-category-10').val();

And remember that input has no text property. Need to use the val method

Upvotes: 1

Related Questions