0

I am a newbie to Jquery as will be evident. I have 2 scripts in an external file named scrip.js and it is included with a simple

<script src="/media/javascripts/scrips.js"></script>

the two scripts in scrips.js are

function textCounter(field,cntfield,maxlimit) {
 if (field.value.length > maxlimit) // if too long...trim it!
 field.value = field.value.substring(0, maxlimit);
 // otherwise, update 'characters left' counter
 else
 cntfield.value = maxlimit - field.value.length;
 }

$(function() {
$("button").click(function(){
$("p").css("color","black");
  });
});

The first script works fine. the second does not. The html for the second scrip looks like this, very simple:

<span><? echo $row->date, nbs(10), $row->author, nbs(20), anchor("http://twitter.com   /home?status=$twittermsg", 'ReTweet', $tweet), nbs(15), "<button>Black Font</button>"; ?></span> 

The button Black font should be selected by the second script in the external file but it doesnt work. Is there something else to be done to a jquery scrip in an external file to make it work? Is there anything I should be doing in the html to get it to work? (the html is Codeigniter btw)

I have read several questions on here about this but they appear very confusing

Thank you

10
  • 1
    The second script is in PHP, not Javascript. Commented Sep 16, 2010 at 1:29
  • Is your .click() event firing? In other words, is the event handler getting set on the correct element and then firing when actually clicked? You can check this by putting something like alert('I work!'); inside the click function. What I am trying to do here is to drill down on the source of the problem by determining how much of the script is working and where the point of failure is. Commented Sep 16, 2010 at 1:34
  • Your right, I never even noticed that as I got it froma javascript depository. But it works. I will take it out Commented Sep 16, 2010 at 1:35
  • Let me try that Jeff after I take the php out of the script Commented Sep 16, 2010 at 1:36
  • No the click event is not firing. Commented Sep 16, 2010 at 1:41

4 Answers 4

2

in your given link, I don't see this somewhere

$("button").click(function(){...})

and you have two link scripts for jQuery, one at the header and one at the bottom part. Just one will do, remove the bottom one.

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

3 Comments

I dont see the script in the source either but it works now. SHould external scripts show up in the source?
It's working now cause you have included lemonrose.net/media/javascripts/scrips.js ;)
Yes, I found a faulty link after I had changed some the external file. I has used the editor and "went back" and left the faulty link in. Thanks very much Reigel
2

Did you make sure to include the jquery.js file before you included your script like below:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="/media/javascripts/scrips.js"></script>

1 Comment

Yes, that script comes first, links to google ajax jquery script
1

See the error console for js error. I did not find any problem in your script. You can use an alert in button click function to debug your script. you can use button and p id/class in order to avoid conflict.

Comments

0

Looks to me like you're missing an ending brace for the function...

The last brace that I see ends the if statement. I believe you need another one to end the function as well:

function textCounter(field,cntfield,maxlimit) {
   if (field.value.length > maxlimit) // if too long...trim it!
       field.value = field.value.substring(0, maxlimit);
       // otherwise, update 'characters left' counter
   else
       cntfield.value = maxlimit - field.value.length;
   }
}

4 Comments

I took that function out of the external file and put it back in the head. Let me look at that. that script does work fine though
@Keith what? now you have 3 braces....
LOL I tried writing it correctly with all the brackets and it didnt work, so i left it back at 2
No need for a third brace since you don't need braces for an if-else when there is only one statement under an if or an else statement

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.