Getting the value of inputs

Take a look at this fiddle: here.

I am trying to grab the values of the inputs with the jquery code you see.
Is there anything wrong with this specific code?

It’s because both your inputs have the same class name. The JS doesn’t know which one is being referred to. Normally, you’d use the ID property for JS selections, as they have to be unique, that way the JS can be used to ‘inspect’ its value.

Also, I think you need to look at how your HTML is structured, as I think you need a little more than you have create a mutually exclusive yes/no option.

1 Like

what is wrong with that?
the code works perfectly OK…I was just asking anyway…but the bottom line is that I CAN get the value that the user selected.

You’ll have to excuse me, diagnostics on a phone from a hospital bed has it’s pitfalls…

Well there is no checked input element on the page, so the console just logs undefined; and when the user at some point makes a selection, the script has long terminated. However, you can add event listeners to those elements:

$('.price_show').on('change', function () {
  console.log(this.value)
})

These will keep listening to “change” events, and execute the 2nd argument function whenever one is triggered.

1 Like

You are right about event listeners…I do not use the change event though…I do something else-let me explain.
This is data the needs to go to the server as part of an ajax request.This request is triggered when the user clicks a button:

$('#saveserv').click(function(e){
 var priceshow=$('.price_show:checked').val();
//ajax request 
})

so the only handler here is the click of the button.Tell me what you think.If the user chooses another radio button then clicking the save button will take into account this change.

So… what’s the problem?

I never said that necessarily there was a problem…I just want to hear some views.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.