0

This post was on the right track, but I need one step further; my input names potentially have non-sequential index values assigned to them.

Background: Instead of a long series of checkboxes, I have a routine that builds a list from selecting values in a dropbox. This helps reduce real estate on the screen. So as the user selects values from the drop down, the script tries to make sure the selection does not already exist, and if it does, don't add it a second time.

The list generated looks like this:

<div id="Access:search_div">
<ul class="options">
<li onclick="removeRoleOptions(this,'Access:search_div');"><input type="hidden" name="Access:search[7]" value="1" />Standard</li>
<li onclick="removeRoleOptions(this,'Access:search_div');"><input type="hidden" name="Access:search[8]" value="1" />Premium</li>
<li onclick="removeRoleOptions(this,'Access:search_div');"><input type="hidden" name="Access:search[10]" value="1" />Lifetime</li>
<li onclick="removeRoleOptions(this,'Access:search_div');"><input type="hidden" name="Access:search[14]" value="1" />SysOp</li>
</ul>
</div>

I see plenty of examples of how to find the value, but it's just a Boolean, so if it's in the list, it will always be 1. What I need is to search the name and see what index value is attached. So for the above code, I want to compare the value of the item selected from the drop down against 7, 8, 10, and 14.

Here is the important part of the code I have so far:

function addRoleOptions(select, variable, output) {
var display=document.getElementById(output);
var option = select.options[select.selectedIndex];
var ul = display.getElementsByTagName('ul')[0];

if (!ul) {
    ul = document.createElement("ul");
    ul.setAttribute('class', 'options');
    display.innerHTML = '';
    display.appendChild(ul);
}

var choices = ul.getElementsByTagName('input');

for (var i = 0; i < choices.length; i++) {
    if (choices[i].value == option.value) {
        return;
    }
}

option.value holds the number I'm comparing against.

So my question is, how do I parse out the value within the [] of the name to compare against the value being sent?

Community
  • 1
  • 1
MivaScott
  • 1,763
  • 1
  • 12
  • 30

2 Answers2

0

I am not so sure about the requirement, but then you can try using regex to extract the index from the name.

(choices[i].name).match(/[0-9]+/g) will give you the value between the []

and then you can use that value to the operation you need to do

swapnilsarwe
  • 1,290
  • 1
  • 8
  • 13
0
        var s = "Access:search[6543]";
        var num = s.match(/\d+/)
Michael Besteck
  • 2,415
  • 18
  • 10