...

View Full Version : Help adding id's to my function



penny94
11-06-2012, 03:57 AM
Hi,

I have a piece of code that works fine on a single drop down box.

However I need to use it within a page that has mulitple drop down boxes each with their own ID.

So how do I add the ID after 'selectBox'.


<script type="text/javascript">

function changeFunc() {

var selectedValue = selectBox.options[selectBox.selectedIndex].value;

if (selectedValue == 2)
{
alert(selectedValue);
}
}

</script>

I have used the id's before like below but don't understand how to implement them in the code above.



document.getElementById("hideabr_"+id).style.display="inline";

Thanks

Old Pedant
11-06-2012, 04:33 AM
Where, pray tell, are you getting selectBox from in the first place?

Generally, for form elements, you should NOT use their ID. You should use their name.

So it depends on whether you really want to use the ID or whether, better, you use the name.

You also don't say where the numeric part of the ID/name from. I have chosen to pass it into the function as the argument to the function, which.

I am assuming you have either <select id="selectBox73"> or <select name="selectBox28"> (or whatever number you are using).

ID:


function changeFunc( which ) {
var sel = document.getElementById("selectBox" + which);
var selectedValue = sel.value;
if (selectedValue == 2) { alert(2); }
}


Name:


function changeFunc( whiich ) {
var frm = document.getElementById("idOfYourForm");
var sel = frm["selectBox" + which];
var selectedValue = sel.value;
if (selectedValue == 2) { alert(2); }
}

penny94
11-06-2012, 09:56 AM
Thanks I got it working!

The ID was coming from a PHP loop:


<select name="byon" class="hidespan" onchange="byonFunc('<?php echo $id;?>');" id="dotxtdate_<?php echo $id;?>">
<option value="by">By:</option>
<option value="on">On:</option>
<option value="2">None</option>
</select>

Working code:

function byonFunc(id) {

var sel = document.getElementById("dotxtdate_"+id);
var selectedValue = sel.value;
if (selectedValue == 2) { alert(2); }

}

Thanks for your help

penny94
11-06-2012, 09:57 AM
Thanks I got it working!

The ID was coming from a PHP loop:


<select name="byon" class="hidespan" onchange="byonFunc('<?php echo $id;?>');" id="dotxtdate_<?php echo $id;?>">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>

Working code:

function byonFunc(id) {

var sel = document.getElementById("dotxtdate_"+id);
var selectedValue = sel.value;
if (selectedValue == 2) { alert(2); }

}

Thanks for your help



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum