Java Script: obtenga la cadena equivalente a una ID de elementos

my naming convention

id=xxxxx //actual field shown in the screen
id=xxxxxHDN // hidden field containing the enable/disabled status of the component from the set from the controller.

Now what I am trying to do is get the satus of xxxxxHDN to be true/false , and accordingly set the components status to disabled /enabled .with java script..

var div = document.getElementById("hiddenFields"); // i hava some 30 hidden fields       containing the 
var j;
    for (j=0;j<div.childNodes.length;j++)
if(div.childNodes[j].value){
    alert("inside the loop");
    var someElementHDN = div.childNodes[j].id;     // my aim is to get the ID=xxxxxHDN
    var someElementHDNToString = someElementHDN .toString(); // my aim is to get the string value "xxxxxHDN"
    var toRemove = 'HDN'; // the part i wanna remove from 'someElementHDNToString' to make it an id for 'xxxxx'
    var equivalantComponentIDAsString = someElementToString.replace(toRemove,'');
    $('#' + equivalantComponentIDAsString ).attr('disabled', true);
    }
}

Invested a lot of time manupulatiing things above , doesent seems to work . I am new to java scrcript , Where am I missing it?

preguntado el 04 de diciembre de 12 a las 04:12

Why this magic number 10 used to initialize the loop variable j? -

Ramesh Thats a miss ,while I was debugging ,for not to get in the loop for multiple times while , I will edit the question , thank you -

@Ramesh Looks like he already is (last two lines of the loop). -

In which browser does a DIV element have a length attribute? -

Donde hace someElement come from? If it's a DOM element, where is the toString method in the DOM Interfaz HTMLElement? Only a small number of elements have a value attribute, what type of elements are you dealing with? -

2 Respuestas

If you have an element with id like 'fooHDN' and want to find another element with id 'foo', then you can do something like:

var otherElement = document.getElementById(someElement.id.replace(/HDN$/,''));

Assuming that you already have someElement and it's a DOM element.

Respondido el 04 de diciembre de 12 a las 04:12

Your posted js code has error: div does not have a "length", do you mean "div.childNodes.length"? Anyway, since you're using jQuery already, I think it can become easier as below: Already tested and it works fine.

$("#hiddenFields input[type='hidden'][id$='HDN']").each(
    function () {
        var elemId = this.id.replace(/HDN$/, '');
        $('#' + elemId).attr('disabled', this.value.toLowerCase() == 'false' ? false : true);
    }
);

Respondido el 04 de diciembre de 12 a las 04:12

Hi Teddy, your code is not taking care of problem such as HDN repeating more than once in the hidden field ID - Ramesh

@Ramesh Hi, be aware that "id$='HDN'" means only to find those ends with HDN, same as the regular expression "/HDN$/". So it's already able to handle the repeating case. - Osito de peluche

Got it... Thanks for explaining. - Ramesh

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.