3

i have a problem with this code line:

alert($('input' + ' #' + $id_bici).attr('value'));

the HTML code is:

<input type="hidden" id="id_bici1" name="id_bici1" value="9">

the alert result is "undefined"

5 Answers 5

8

You have a space between input and #. If you want the input with the specific id. Then there should not be a space between them.

var $id_bici = 'id_bici1'; //added for full example
alert($('input' + '#' + $id_bici).attr('value'));
Sign up to request clarification or add additional context in comments.

1 Comment

And because it's an ID, and ID's are unique, you can drop input entirely from the selector and just use the ID.
1
var yourId = 1;
alert($('input' + '#id_bici' + yourId).attr('value'));

with yourId is 1 in your sample code.

Comments

1

You could do this:

alert($('#id_bici1').attr('value'));

Comments

1

There's a space too much:

alert($('input' + ' #' + $id_bici).attr('value'));

should be

alert($('input' + '#' + $id_bici).attr('value'));

Comments

1

Element1 Element2 is a selector that selects all Element2 inside Element1. So, "input #id" will select a element with id inside inputs. It's not your case. You already know the element id. Just use it.

$("id_bici1")

See CSS Selector Reference for a basic list of selectors.

var $id_bici = "id_bici1";
    alert($('#' + $id_bici).attr('value'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" id="id_bici1" name="id_bici1" value="9">

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.