-1

I am trying to apply javascript twice to a simple html textarea. I have onfocus and autogrow (which is actually jquery). It seems i can only get one or the other to work but never both at once.

My script has an:

<?php
include 'header.php';
?>

This seems to be the problem. In both the header.php and the index.php (where the header is included) I have loaded jquery.js. when I remove this file from the header, autogrow works but not my onfocus event (which clears the default text). Is there a way to include another file without the two effecting each other. I cant provide the code because it too long.

This is my onfocus code:

<script type='text/javascript'>
    function addEvents(id) {
var field = document.getElementById(id);
field.onfocus = function () {
    if (this.value == "Answer this problem...") {
        this.value = "";
    } 
};
field.onblur = function () {
    if (this.value == "") {
        this.value = "Answer this problem...";
    } 
};

} addEvents("answerbox");

3
  • 3
    You have to post more code for us to be able to help. Loading jQuery twice will fail because it tries to redefine the same functions. Commented Jun 16, 2011 at 11:12
  • my scripts too have <?php ****** ?>, this doesnt show us the full problem definition, please clarify a bit more... Commented Jun 16, 2011 at 11:13
  • @devraj trust me you dont wanna sort through the code. thanks for the advice however the header.php and index.php each need to use jquery seperatly. how can i only load it once? Commented Jun 16, 2011 at 11:14

4 Answers 4

2

Rule number 1 : Load jquery only once!. You are loading it twice to the same page. So remove it from one file which loads later.

I assume the first thing you do in your index.php is include header.php, so remove jquery loading from index.php.

Sign up to request clarification or add additional context in comments.

1 Comment

i moved both jquery.js and autogrow.js to header.php and removed them all from index.php. i got autogrow to work however now, the onfocus clear text wont work
1

Have your script in a seperate file and include in each page using php include_once function.

create a file jquery.php and add the following.

<script type='text/javascript' src='jquery.js'></script> <script type="text/javascript" src="autogrow.js"></script>

and in header.php

include_once('jquery.php');

and in index.php

include_once('header.php');
include_once('jquery.php');

4 Comments

@jagadeesan yes i have used include_once and scripted jquery.js. Should I try possibly renaming the jquery file?
I actually used the code below to load autogrow and jquery (i used this method to load jquery in both files)
<script type='text/javascript' src='jquery.js'></script> <script type="text/javascript" src="autogrow.js"></script>
thanks. i got the jquery stuff to work, but for some reason now my javascript onfocus doenst work when i click the box. i dont konw why the two events are related
1

this thread might answer your question

Check if jQuery is included in Header (Joomla)

Comments

1

It's got nothing to do with PHP include files. And jQuery is smart enough to work itself out if you include it multiple times.

It's the autogrow plugin code which is dodgy.

Edit the query.autogrowtextarea.js file. Change line this.onfocus = grow; to jQuery(this).focus(grow);.

And then your Javascript code, I've changed it to use the jQuery library (mainly because it's alot less code to make it compatible across older browsers like IE6+).

function addEvents(id) {
    var field = jQuery('#' + id);
    field.focus(function () {
        if (this.value == "Answer this problem...") {
            this.value = "";
        }
    });

    field.blur(function () {
        if (this.value == "") {
            this.value = "Answer this problem...";
        }
    });
}
addEvents("answerbox");
</script>

Both should work at the same time now.

3 Comments

what about onblur and I apologize for being an amateur but what should go in yourHandlerHere?
<script type='text/javascript'> function addEvents(id) { var field = document.getElementById(id); field.onfocus = function () { if (this.value == "Answer this problem...") { this.value = ""; } }; field.onblur = function () { if (this.value == "") { this.value = "Answer this problem..."; } }; } addEvents("answerbox"); </script>
i have posted the code in my original question for easier viewing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.