views:

515

answers:

6

I have a jquery question about selecting Id.

Basically, I call a javascript function from an onClick function which I pass in control Id and LabelId.

If I use document.getElementById, it will work, however, if I use jQuery selector, it's NOT working.

<script type="text/javascript">
    jQuery.noConflict();
    function ToggleProgressEnable(valueofRadio, controlId, labelId) {

//Comments: the following will work.  
//        var control = document.getElementById(controlId);
//        var label = document.getElementById(labelId);

//The following is not working.
        var control = jQuery("'#" + controlId + "'");
        var label = jQuery("'#" + labelId + "'");
        if (control != null && label!=null) {
            //alert(control.Id);
            //alert(control.disabled);
            if (valueofRadio == "yes") {
                control.disabled = false;                
                label.disabled = false;
            }
            else if (valueofRadio == "no") {
            control.disabled = true;
            control.value = "";
            label.disabled = true;
            }
            //alert(control.disabled);
        }
    }    
</script>
+6  A: 
var control = jQuery("'#" + controlId + "'");        
var label = jQuery("'#" + labelId + "'");

you're doing your selectors wrong. get rid of your single quotes and just use your double quotes:

var control = $("#" + controlId);        
var label = $("#" + labelId);
Jason
+1 - Maybe show the correct usage instead of quoting the wrong one?
Philippe Leybaert
definitely. it looks like he's composing the source code instead of the needed value.
Javier
yeah sorry, updated to reflect correct usage :)
Jason
when use jquery select id, did it return array or the single element?
J.W.
technically it returns an array, but if there's only one element in the array (for instance, if you're using best practices and have one unique element per page) then it will return that single element. if you do something like $("a") it will get all your <a> tags.
Jason
+1  A: 

You are receiving a jQuery object, not a DOM element. You should use:

control.val()

for example to get the value...

Oh and i second Jasons answer:

jQuery('#' + controlId);

should do fine...

Ropstah
Downvote, no comment? What's going on?
Ropstah
i didn't downvote you, but you have a . where there should be a +
Jason
Wasn't blaming you, just the anonymous voter... Sorry for the . too much php today :)
Ropstah
A: 
    var control = jQuery("#" + controlId);
Flavius Stef
A: 

I would suggest passing the controlId and labelId as selectors or elements rather than as strings. Take full advantage of the fact that you can pass objects as arguments.

var control = $('#myControl');
var label = $('#myLabel');

ToggleProgressEnable(5, control, label);
Soviut
+1  A: 

Maybe this:

var control = jQuery("#" + controlId);
var label = jQuery("#" + labelId);
sasa
var label won't work because you still have a single quote...
Jason
it will work, now :)
sasa
+2  A: 

Try

jQuery('#' + controlId);

Otherwise, you're searching for '#controlId', which is not a valid selector.

Adrian Godong