views:

71

answers:

3

The idea: I'm setting the value of an input with type="hidden" via regular Javascript or jQuery.

The issue: neither jQuery nor document.getElementById will find the hidden input, even though I'm absolutely sure the selector is correct and there are no conflicting elements.

The code: I can't really post much of it, because it's full of rather complicated PHP that confuses me when I just look at it.

Here's the javascript:

$("#" + input.id.substr(0,2) + "_budget_hidden").val(budg_total);

Note: there's nothing wrong with the selector, and the "input" is a different element that I'm using to reference the hidden.

Here's the HTML:

<input type="hidden" name="s<?=$step_counter?>_budget_hidden" 
       id="s<?=$step_counter?>_budget_hidden" value="0" />

The code is kind of out of context, but it's more of a general problem with Javascript than a syntactical error. Thoughts?

+1  A: 

In $("#" + input.id.substr(0,2) + "_budget_hidden").val(budg_total); you take two chars before the first underscore in your hidden id. However your hidden id have only one char 's'

EDIT

Ok the <?= ?> was hidden before the question edit.

Do you call your script after the body onload event? EX:

$(document).ready(function(){
    $("#" + input.id.substr(0,2) + "_budget_hidden").bind("keyPressed",function(){
        $("#" + input.id.substr(0,2) + "_budget_hidden").val(budg_total);
     }
});
Gregoire
+1 I thought exactly the same
galambalazs
It's being called in the onkeypress event for the "input" variable that I reference in the jQuery selector.
Will
Oh, I see what you're saying.The event is defined inline, on the input itself.onkeypress="updateDifferences(this)"So that shouldn't be an issue, in theory.
Will
@Will how can you have a onkeypress on hidden input? they are not shown.
Gregoire
The onkeypress is on a different text input, which calls a function that changes the hidden input.
Will
A: 

I think that input.id.substr(0,2) says to start at the start of the string, take 2 characters and use that.

http://www.w3schools.com/jsref/jsref_substr.asp

Try using Firebug to see what the result of that method call is.

Mike
Yes, that is what the substring does. As I said, it's a rather complicated system taken out of context, but I guarantee that the particular selector is accurate and corresponds to a hidden form element in the page.I've used Firebug, and there are no errors or warnings.
Will
A: 

FYI: We can get the hidden input value using jQuery, even we can also edit any hidden input value using jQuery.

I think your way of getting the hidden value using 'substr' method is causing some problem. You are using like substr(0, 2) so are sure that the variable $step_variable is a single digit number, otherwise your code will not return correct result.

I am giving some sample code below, check it once.

Here's the javascript:

    var input_id = $("hidden_val").attr("id").substr(1);
    $("#" + input_id + "_budget_hidden").val(budg_total);

Here's the HTML:

    input type="hidden" class="hidden_val" name="s_budget_hidden" id="s" value="0" 

I think this will help you. Let me know if you are not following this flow to solve your issue.

Siva
That is an excellent point, could have caused some potential errors there. I just decided to use explode instead and avoided some nasty trouble.However, the particular input I was experimenting on had a step_counter value of 2, so it wouldn't have mattered anyway. I'm not 110% convinced the selector is right -- I've echoed it out and it shows exactly the same as the id of the hidden input I'm trying to access. It's the jQuery/Javascript, not the selector itself.
Will