The forum of the forums
Would you like to react to this message? Create an account in a few clicks or log in to continue.

button which change the background

3 posters

Go down

button which change the background Empty button which change the background

Post by sample123 June 21st 2013, 11:37 am

Hello, i have this code



Code:
<input type="button"
value="Pattern1"
onclick="document.body.style.cssText+=';background: red;background-repeat: no-repeat;';">



which change the background from black to red when i click it, but i want when i click this button and background becomes red , i want remain red  and when I recharge the forum.
avatar
sample123
New Member

Posts : 13
Reputation : 1
Language : English

Back to top Go down

button which change the background Empty Re: button which change the background

Post by Ultron's Vision June 21st 2013, 12:20 pm

Try setting a cookie with JS. Also, use unobstrusive JavaScript. As in, all click elements are specified in a script and not with onclick="" attributes.

Code:
jQuery(document).ready(function(){

jQuery('input[value="Pattern1"]').click(function(e){

e.preventDefault();
jQuery(document.body).css({
'background-color' : '#ff0000',
'background-repeat' : 'no-repeat'
});

document.cookie = "color=#ff0000";

});

Then, in another JavaScript file.

Code:
jQuery(document).ready(function(){

var myGetCookie = document.cookie.replace(/(?:(?:^|.*;\s*)color\s*\=\s*([^;]*).*$)|^.*$/,"$1");
var testRegex = /#[abcdefABCDEF0-9]{6}/;
if(testRegex.test(myGetCookie)) {
jQuery(document.body).css({
'background-color' : myGetCookie,
'background-repeat' : 'no-repeat'
});
}

});

Hope this helped.


Last edited by Ultron's Vision on June 21st 2013, 1:50 pm; edited 1 time in total
Ultron's Vision
Ultron's Vision
Forumember

Male Posts : 634
Reputation : 45
Language : English | German | HTML | JavaScript | PHP | C++ | Perl | Java
Location : Vienna, Austria

http://duelacademy.net

Back to top Go down

button which change the background Empty Re: button which change the background

Post by sample123 June 21st 2013, 1:13 pm

it doesn't work :/
avatar
sample123
New Member

Posts : 13
Reputation : 1
Language : English

Back to top Go down

button which change the background Empty Re: button which change the background

Post by Ultron's Vision June 21st 2013, 1:51 pm

I edited my code a bit, my RegExp only matched numbers and not hexadecimals, but it should be fixed now.
Ultron's Vision
Ultron's Vision
Forumember

Male Posts : 634
Reputation : 45
Language : English | German | HTML | JavaScript | PHP | C++ | Perl | Java
Location : Vienna, Austria

http://duelacademy.net

Back to top Go down

button which change the background Empty Re: button which change the background

Post by sample123 June 21st 2013, 2:17 pm

what i must write in php ?

doesn't work again :/
avatar
sample123
New Member

Posts : 13
Reputation : 1
Language : English

Back to top Go down

button which change the background Empty Re: button which change the background

Post by Ultron's Vision June 21st 2013, 3:32 pm

Who said anything about PHP? O_o
You're using a forumotion-hosted forum, right?
If you can use PHP, this code would be written totally different (and you wouldn't be using a forumotion forum).
Ultron's Vision
Ultron's Vision
Forumember

Male Posts : 634
Reputation : 45
Language : English | German | HTML | JavaScript | PHP | C++ | Perl | Java
Location : Vienna, Austria

http://duelacademy.net

Back to top Go down

button which change the background Empty Re: button which change the background

Post by sample123 June 21st 2013, 3:36 pm

My friend, i want create one button which it can change the color of background etc.

i have created this button with "onclick" but when i do recharge the changes Leave...


I meant the templates when i said "php".
avatar
sample123
New Member

Posts : 13
Reputation : 1
Language : English

Back to top Go down

button which change the background Empty Re: button which change the background

Post by Ultron's Vision June 21st 2013, 4:55 pm

Did you add the JavaScript I provided under Admin Panel -> Modules -> HTML & JavaScript -> JavaScript pages management -> Create a new JavaScript?

Otherwise, I'll provide some more code later on. You might need to enable cookies in your browser.
Ultron's Vision
Ultron's Vision
Forumember

Male Posts : 634
Reputation : 45
Language : English | German | HTML | JavaScript | PHP | C++ | Perl | Java
Location : Vienna, Austria

http://duelacademy.net

Back to top Go down

button which change the background Empty Re: button which change the background

Post by sample123 June 21st 2013, 5:29 pm

i add this code. but nothing.
avatar
sample123
New Member

Posts : 13
Reputation : 1
Language : English

Back to top Go down

button which change the background Empty Re: button which change the background

Post by sample123 June 22nd 2013, 2:56 pm

Bump
avatar
sample123
New Member

Posts : 13
Reputation : 1
Language : English

Back to top Go down

button which change the background Empty Re: button which change the background

Post by SLGray June 22nd 2013, 4:54 pm

Please don't double/triple post. Your post need to be separated by 24 hours before bumping, replying or adding more information. Please use the edit button instead!


button which change the background Slgray10

When your topic has been solved, ensure you mark the topic solved.
Never post your email in public.
SLGray
SLGray
Administrator
Administrator

Male Posts : 51463
Reputation : 3519
Language : English
Location : United States

https://forumsclub.com/gc/128-link-directory/

Back to top Go down

button which change the background Empty Re: button which change the background

Post by Ultron's Vision June 22nd 2013, 5:00 pm

I shall perform some tests with the code and then I'll get back to you, okay?
Ultron's Vision
Ultron's Vision
Forumember

Male Posts : 634
Reputation : 45
Language : English | German | HTML | JavaScript | PHP | C++ | Perl | Java
Location : Vienna, Austria

http://duelacademy.net

Back to top Go down

button which change the background Empty Re: button which change the background

Post by sample123 June 22nd 2013, 5:54 pm

ok my friend Smile
avatar
sample123
New Member

Posts : 13
Reputation : 1
Language : English

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum