My website lists records for sale.
I have an html form with several fields as follows:
<input type="text" name="artist" id="artist" class="txt" />
<input type="text" name="title" id="title" class="txt" />
<input class="tracks" name="a1" id="a1" >
<input class="tracks" name="a2" id="a2" >
<input class="tracks" name="a3" id="a3" >
$tit="random title";
I want to have one button which calls a javascript function that replaces each form fields' text (if it contains the records title ($tit) to !! if the field name matches a list of form field names that are predefined in a tracks array this:
var tracks= ['a1', 'a2', 'a3' ];
I had a look at: // populating form fields with array
The code Ive got so far allows the change button below to replace the title in form field a1 to !! if the title text is found in form field a1 but i want the js code to loop through all form fields in the array and replace the title if its found.
<input type="button" class="button-8" value="change" name="no" onclick="spling('a1','<?php echo $tit; ?>');">
<script>
function spling(v,t) {
// v= side a1
// t= record title $tit
var txt=document.getElementById(v).value;
var uc= t.toUpperCase()
//if (txt.indexOf(t) >= 0){
if (txt.toUpperCase().indexOf(uc) >=0) {
txt =txt.replace(t,'!!');
document.getElementById(v).value=txt;
}
else{
document.getElementById('res').innerHTML = t+" not found in field "+v;
}
}
</script>
I need to be able to replace the records title with !! if its found in a1,a2,a3
I want the js code to loop through all form fields in the array and change the text with one button click rather than having three separate buttons to replace the title text in fields a1,a2 and a3.
Thanks for your help!
The button replaces the title text in form field a1 to !! if field a1 contains the $tit text already.
I've googled for a solution but find myself joining snippets of code together and not getting the result I want.