Jump to content

Problem executing function


Recommended Posts

Hello everyone, I'm having a problem executing a function, I have now tried different methods for hours and can't understand anything anymore.

 

Now when I execute the fuction infoApp(); in this function below, then it works.

 function editCat(ids){  $('.mode').hide();  $('.editmode_'+ids).show();   $('.save_category_'+ids).click(function(){    var getThisName = $('.category_'+ids).val();    infoApp('cat_edit',getThisName);   }); }

But when I try to execute the funtion automaticly then it does not work.Like this:

var getThisName = 'test value';infoApp('cat_edit',getThisName);
Here's the function
 function infoApp(app,usedata){  $('.infomsg').show();    if(app == "cat_edit"){      $('.infomsg').text("Information, "+ usedata +" category was saved");    }   $('.infomsg').fadeOut(3600); }

Hope anyone got some advice for me.Kris

 

Link to post
Share on other sites

What do you mean it doesn't work, what does it do? Are you checking for error messages on the console?

It is not executed at all. No error messages or warnings

Link to post
Share on other sites

The browser isn't just going to not do something that you tell it to do, and also not show an error. You can add alerts or console.log statements if you want to verify things. Put one before you call the function so that you can verify that you're actually calling it, put another one as the first line of the function so you can verify that it's running.

Link to post
Share on other sites

Thanks, It does execute, but it does not load the data or show the "dialog"/div.I checked if it passes the cat_edit and usedata values and it does. but still no error messages on why it is not executing the jquery show();

Link to post
Share on other sites

The first rule is to stop assuming that it's simply not executing things, and start considering why you're not seeing the behavior that you think you should. It IS executing the show method. Have you verified that $('.infomsg') actually returns one or more HTML elements that are found on the page? You can use console.log to see what that returns.

Link to post
Share on other sites

I will remember to add alerts/log statements to my functions.Yeah, as I said, it works fine in the function above. where I call the exact same function, just different data.

Link to post
Share on other sites

I thought it could be because I'm triggering the function manually here?

$('.save_category_'+ids).click(function(){var getThisName = $('.category_'+ids).val();infoApp('cat_edit',getThisName);});
Link to post
Share on other sites

Ok, I added this line and got the value -1 from the log, what does that mean? that there is no element found?

console.log($('.infomsg').index());

Edit; I added style to the element and tried to retrive it, but it returns undefined.

console.log($('.infomsg').index() + '' +  app + usedata + '' + $('.infomsg').attr('style'));

I don't know if that is because of the code it self being faulty, but if not then i guess there is no element found? :sEdit 2; console value

-1 cat_edit lol undefined 

Edit3; log value from the first function i posted here, where the function works,

8 cat_edit Electronics font-size: 12px; display: block; 

I believe that answers the question and that the element is not found when the function is automaticly executed.Should I append a element in the function itself instead of having it outside and hidden to solve the issue?

Edited by DIY-Forum
Link to post
Share on other sites

This is very weird, in the source now I have the element with the changed value, but still not able to retrive it's data to the log or show it on the page.

It seems as the element I autoload is always hidden, while the manually loaded one is fading to 0 opacity before changing to hidden.

 

I decided to check weather it was the fact that I was not triggering the function manually, and it seems that the right way to trigger it automaticly is to do a setTimeout or interval for jQuery to respond.

 

Could that be right? as this works;

 

setTimeout("infoApp('cat_edit',getThisName)",3600);

And this dont?

infoApp('cat_edit',getThisName);

thanks alot for the help on logging, it will really help me in the future! note, the returned log value is still -1 and undefined on style when setTImeout is loading, so I'm not much wiser, though it works :)

 

 

Kris

Edited by DIY-Forum
Link to post
Share on other sites

Code like yours would make much more sense if you could create a small self-contained example that included the relevant HTML elements.

 

I tried to mock up such a page, but I can only guess what you are trying to do. The function works fine, but you are using classes for some things that I would expect to be unique and more properly identified with an id rather than a class.

Link to post
Share on other sites

Only thing left out for the future to work is this: <div class="infomsg"></div> had hoped that would be clear with the $('.infomsg')But will remember to add a complete example for my problem next time, understand it is easier to help when not having to code everything else than the problem too be able to help. :)

 

Thanks.

 

Kris

Link to post
Share on other sites

I had this...

<!DOCTYPE html><html lang="en"><head><title>jQuery</title><style>div{border: 1px solid #eee;}</style><script>window.onerror = function(m, u, l){alert('Javascript Error: '+m+'nURL: '+u+'nLine Number: '+l);return true;}</script><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script><script>$(document).ready(function(){function editCat(ids){  $('.mode').hide();  $('.editmode_'+ids).show();   $('.save_category_'+ids).click(function(){    var getThisName = $('.category_'+ids).val();alert('len = '+ getThisName.length +'ngetThisName = ['+ getThisName +']');    infoApp('cat_edit',getThisName);   }); }function infoApp(app,usedata){  $('.infomsg').show();    if(app == "cat_edit"){      $('.infomsg').text("Information, "+ usedata +" category was saved");    }   $('.infomsg').fadeOut(3600);}editCat(1);alert('ok');var getThisName = 'test value';infoApp('cat_edit',getThisName);}); </script></head><body><div class="mode editmode_1">mode editmode_1</div><div class="save_category_1">save_category_1</div><div class="infomsg"></div><input type="button" id="btn1" class="save_category_1" value="btn1"/><input type="button" id="btn2" class="save_category_1" value="btn2"/><input type="button" id="btn3" class="category_1" value="btn3"/><input type="button" id="btn4" class="category_1" value="btn4"/></body></html>
Link to post
Share on other sites

Wow! :) You are engaged in helping I see, that is awesome!! The first function was just there to show you that I was triggering the function from jquery and that that worked, If I knew anyone would try to code the entire function I would probably have added a shortend version of my own! :)The first function is for informing that a category has been edited and that works fine :) but the second code i posted

var getThisName = 'test value';infoApp('cat_edit',getThisName);

which was originally inside a

//php execution<?php if($_GET['returndata']=="showorderup"){?>var getThisName = 'test value';infoApp('cat_edit',getThisName);<?php }?>

is the one i needed help with,so if I were to post again I would just give you this:

<script>$(document).ready(function(){  $('.infomsg').hide();});</script><script>var getThisName = 'test value';infoApp('cat_edit',getThisName);function infoApp(app,usedata){  $('.infomsg').show();   if(app == "cat_edit"){    $('.infomsg').text("Information, "+ usedata +" category was saved");   }  $('.infomsg').fadeOut(3600); }</script><body><div class="infomsg"></div></body>

And add that when the function infoApp is triggered within a jquery.click function it is working fine.Sorry for not taking time to make it simple from the start.

 

Kris

Edited by DIY-Forum
Link to post
Share on other sites

The reason the code above doesn't work is because it hasn't created the div by the time your Javascript code runs. You need to use a document ready handler if your code is trying to access elements on the page, so that your code runs after the page finishes loading.

Link to post
Share on other sites

Aha! so that is why it worked with the delay/settimeout.. I will be more patient with my functions(give them time to load), clean up my code's before asking questions about them, and start logging and tracing the problem on my own before posting! thanks again, yet another day without a long lasting problem :) Kris

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...