943,547 Members | Top Members by Rank

Ad:
Feb 22nd, 2009
0

Specifying the bg & hover images of a div using the value supplied in a text box

Expand Post »
Hello

I want that the user should specify the full url to 2 images in 2 text boxes, and these images should be set as the hover image and background image respectively, for a div.

How will this be coded

Regards
Arvind
Reputation Points: 10
Solved Threads: 0
Light Poster
arvindikchari is offline Offline
41 posts
since Jan 2009
Feb 22nd, 2009
1

Re: Specifying the bg & hover images of a div using the value supplied in a text box

javascript Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title>test</title>
  4. <script type="text/javascript">
  5.  
  6. function applyStyle() {
  7. var bgText = document.getElementById('bg').value;
  8. var hoverText = document.getElementById('hover').value;
  9. var myDiv = document.getElementById('mydiv');
  10.  
  11. myDiv.style.backgroungImage = "url(" + bgText + ")";
  12. myDiv.onmouseover = function() {
  13. alert(hoverText);
  14. myDiv.style.backgroungImage = "url(" + hoverText + ")";
  15. }
  16.  
  17. myDiv.onmouseout = function() {
  18. alert(bgText);
  19. myDiv.style.backgroungImage = "url(" + bgText + ")";
  20. }
  21. }
  22. </script>
  23.  
  24. <style>
  25. #mydiv {
  26. border: 1px solid black;
  27. width: 100px;
  28. height: 100px;
  29. }
  30. </style>
  31. </head>
  32.  
  33. <body >
  34. <label>BG:</label><input id="bg" type="text"/>
  35. <label>Hover:</label><input id="hover" type="text"/>
  36. <input type="button" onClick="applyStyle()" value="apply">
  37. <div id="mydiv"/>
  38. </body>
  39. </html>
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006
Feb 23rd, 2009
0

Re: Specifying the bg & hover images of a div using the value supplied in a text box

Hello

I created a page with the above code in it, but nothing happens, only alert messages are shown with the url value.

Regards
Arvind
Reputation Points: 10
Solved Threads: 0
Light Poster
arvindikchari is offline Offline
41 posts
since Jan 2009
Feb 23rd, 2009
1

Re: Specifying the bg & hover images of a div using the value supplied in a text box

lines 14 and 19 have a typo

yDiv.style.backgroungImage

should be

yDiv.style.backgroundImage
Reputation Points: 12
Solved Threads: 2
Newbie Poster
scrager is offline Offline
10 posts
since Feb 2009
Mar 23rd, 2009
0

Re: Specifying the bg & hover images of a div using the value supplied in a text box

Hello,

I have a div with the following code:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div class="dr">
  2. <div class="dc m_r m_pad" >
  3. <div class="m3_dr m3_pad"><span class="m3_h_text">featured products</span></div>
  4. </div>
  5. </div>

I want to change the bg image for the div ' m3_dr', the css for which is given below:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. .m3_dr { background:url(images/m3-dr.gif) repeat-x top #FFBF00;}

But the above code is not working, do I have to assign an ID to the div, how should this be coded to make it work

Thanks and Regards
Arvind
Reputation Points: 10
Solved Threads: 0
Light Poster
arvindikchari is offline Offline
41 posts
since Jan 2009
Mar 23rd, 2009
0

Re: Specifying the bg & hover images of a div using the value supplied in a text box

yes u need id.
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006
Mar 25th, 2009
0

Re: Specifying the bg & hover images of a div using the value supplied in a text box

Hello

I added ID to the div, but nothing happens,the code is as below:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function applystyle_mandiv() {
  2.  
  3. var bgText = document.getElementById('s_bkbgiu').value;
  4.  
  5.  
  6.  
  7. var myDiv = document.getElementById('mandiv');
  8.  
  9.  
  10.  
  11. myDiv.style.backgroundImage = "url(" + bgText + ")";
  12.  
  13.  
  14. var manName = document.getElementById("in_manImage");
  15. manName.value = "url(" + bgText + ")";
  16.  
  17.  
  18.  
  19. }

The HTML code is as below:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div id="mandiv" class="dc m1_t" >
  2. <div class="dt m1_b" >
  3. <div class="dr">
  4. <div class="dc m1_pad" >
  5. <span class="m1_h_text">Manufacturers:</span><br>
  6. <img alt="" src="images/spacer.gif" width="1" height="12"><br>
  7. <select class="sel1"><option>&nbsp; Sony</option></select>
  8. </div>
  9. </div>
  10. </div>
  11. </div>

Finally the CSS code is given below:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. .m1_t { background:url(images/m1-t.jpg) no-repeat top;}

Can someone tell me if there is anything wrong with this code, and what changes are required to make it work

Thanks and Regards,
Arvind
Reputation Points: 10
Solved Threads: 0
Light Poster
arvindikchari is offline Offline
41 posts
since Jan 2009
Mar 25th, 2009
0

Re: Specifying the bg & hover images of a div using the value supplied in a text box

There is no HTML element with 's_bkbgiu' id.
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Disappearing List Bug
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: window not resizing?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC