Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My function has something wrong :( 2

Status
Not open for further replies.

Sleidia

Technical User
Joined
May 4, 2001
Messages
1,284
Location
FR
Hi guys :)

I've spent all day long on this function yesterday, and there is no way I can make it do what's it's supposed to do, which is :

dynamically update the text string while data is typed in the form fields.

If you test it, you'll see that it works only with the last form field. Actually, whatever the number of form fields, it's always the last ones that works :(

Some help would be very apreciated ;)

Code:
<html>

<head>

<SCRIPT LANGUAGE="JavaScript1.2">
<!--

if (!string) var string = '<br />{admin_name} {admin_surname},<br /><br />{super_admin_surname} {super_admin_name} has created an admin account for you for the online management of {site_public_name}.<br /><br />The administration interface is located here :<br />{site_private_url}<br /><br />Your login : {admin_log}<br />Your password : {admin_pass}<br /><br />Additional comment from {super_admin_surname} {super_admin_name} :<br /><br />{super_admin_comment}<br /><br />';

if (!string_new) var string_new = string;

function message_preview() {

var data_names_array = new Array(
'admin_name',
'admin_surname',
'admin_log'
);

    for( var j=0; j < data_names_array.length; j++ ) {

    var field_name = 'form_' + data_names_array[j];
    var field_value = eval('document.form_admins_register.'+field_name+'.value');

    var string_to_replace = '{'+field_name.replace('form_', '')+'}';
    string_new = string.replace(string_to_replace, field_value); 

    }

document.getElementById( 'preview' ).innerHTML = string_new;

}

// End -->
</SCRIPT>

</head>

<body>

<form name="form_admins_register">

<input type="text" name="form_admin_surname" onkeyup = "message_preview()"><br />
<input type="text" name="form_admin_name" onkeyup = "message_preview()"><br />
<input type="text" name="form_admin_log" onkeyup = "message_preview()"><br />

</form>

<br />
<br />

<div id="preview" style="
-moz-box-sizing: border-box;
"></div>
    
</body>

</html>
 
If I were you I would just rebuild the entire string in the function - it's that *that* long of a string, so it won't really take much time. From there just stick in the values you want changed at each point in the message as the string is being re-generated. Using the replace method is going to have huge potential to screw up because any strings that are duplicated in the rest of the string will be replaced as well.

-kaht

Looking for a puppy?

[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
 
you are referencing the variable named "string". you are then looping through your three field names (in the array), and, for each one, are replacing the {} value with the value in the form. however, you are referencing the same, beginning string each time around. there is no storage of the other values.



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
alas,

here's something you can work with:

Code:
function message_preview() {

    var data_names_array = new Array('admin_name','admin_surname','admin_log');
    var temp_string = string;
    
    for( var j=0; j < data_names_array.length; j++ ) {
        my_field_nam = 'form_' + data_names_array[j];
        my_field_val = document.forms['form_admins_register'].elements[my_field_nam].value;
        my_repla_str = "{" + data_names_array[j] + "}";
        temp_string = temp_string.replace( my_repla_str, my_field_val );
    }
    document.getElementById( 'preview' ).innerHTML = temp_string;
}

but for the record, i don't like it. and when i don't like something i wrote, you should definitely be asking questions.



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 

Thanks Kaht :)
Unfortunately, I've simplified the code a lot just for this post and the string must remain like that ... because it's fetched by PHP and there are other things that make it better this way ;)

clflava,

I don't get it :(

Wait! .... you mean that
string_new = string.replace(string_to_replace, field_value);
changes the variable string as well?

 
changes the variable string as well?

It will not modify the contents of the variable "string"

-kaht

Looking for a puppy?

[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
 
hey cory, notice on Sleidia's last post I got 2 smiley faces and you got 1 sad face?

I win

-kaht

Looking for a puppy?

[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
 
the official count is one smile and one wink to my one frown. and i even provided a "solution". however, the frown on the title increases each of our frown counts by 1.

kaht:
[smile][wink][sad]

cory:
[sad][sad]



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 

wow ... thanks a lot clflava !!!
I really wish I could be smarter :( arghhh ...

But why don't you like this code? I don't see what's so wrong with it. Care to exlain??

Just to increase your smiley count : :):):):)

 
i just didn't like mine cause i slopped it together. i would normally take time to make it neat and as concise as possible.

you shouldn't wish to be smarter - just take the time to learn. nobody (with the exception of kaht and myself) were born with a built-in knowledge of javascript. everyone learns the same way you do.

one note: always avoid the eval() function. it's unnecessary, slow, and the "easy way out".

you're welcome.



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 

ahahah .... ok :)
I know now who I can bait with my little stars :)

Thanks again !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top