Page 1 of 3 123 LastLast
Results 1 to 15 of 39

Thread: img src as a variable?

  1. #1
    Join Date
    Jan 2006

    img src as a variable?

    in my javascript i set a variable called vara="whatever.gif"

    I want to be able to use this variable in a href command such as:-
    <img src="vara">

    BUT OF COURSE this syntax is wrong.. How would I define the img src line to use the variable?


  2. #2
    Join Date
    Dec 2003
    Bucharest, ROMANIA
    One solution is to give your image an id:


  3. #3
    Join Date
    Dec 2003
    Bucharest, ROMANIA
    But if you need to change it from a href

    <a href="#" onclick="this.nextSibilig.src=vara;return false"><img src="something.jpg"></a>

    make sure that your variable vara is a global and make sure that there is no empty space (gap) between the two tags (<a><img></a>)

  4. #4
    Join Date
    Jan 2006
    hi kor,
    theres a difficulty with this...

    in the header of html i define the javascript variable and the img src needs to use this... the javascript juggles some if conditions so the <img src needs to somehow call the active variable value.. if it was kor.gif the the <img src line would use this ... if the javascript for some reason defines the variable differently then the <img src would reflect this..

  5. #5
    Join Date
    Dec 2003
    Bucharest, ROMANIA
    let's clarify: To change an attribute you need a method (I have gave it to you). To apply a method you need a function or a statement triggered by an event. Now i have only gave you example of how an event onclick will apply a method. Now... Tell me more about your problem. Which suppose to be that event which will trigger the function? Which will be the element which will nest the event. Is your variable a global one? Give us more details, even a little bit of your code, if any

  6. #6
    Join Date
    Jan 2006
    also when the html runs another program would have already defined the variables in the script (it created the file).

    so in the javascript definitions it would set vara="image1.gif" ...

    some functions also use this variable at times BUT:-

    Like mentioned it would be nice just to have the variable referenced in the
    <img src=vara><br>


    but vara in line above has to have some syntax?

  7. #7
    Join Date
    Jan 2006
    In this simple cut down example i would want to have the variable referenced where i have ????? and in this case it ends up using 10.jpg

    any ideas?

    <script language="JavaScript">
    var vara = "10.jpg"
    <img src="?????"></a>

  8. #8
    Join Date
    Dec 2003
    Bucharest, ROMANIA
    Which will be the event? You need an event and an element which will nest the event. The action... Which will be the action? when user loads the page, when clicks something, when rollover something... The event?

  9. #9
    Join Date
    Dec 2003
    Bucharest, ROMANIA

  10. #10
    Join Date
    Jan 2006
    i just showed you a portion of the html.... all i am asking was if there were nothing else in the html what would we put in the ????? .... to make this little
    html work.. dont worry about other functions etc..

  11. #11
    Join Date
    Dec 2003
    Bucharest, ROMANIA
    It looks like you miss the essence of javascript. You need an event. And you need to reference somehow the element whom attributes you want to change. You can not just "put" a variable somewhere. I guess you need the onload event (which is a window's event) Now the code could be something like this:
    PHP Code:
    <script type="text/javascript">
    onload = function(){
    img id="mypic" src="neutral.jpg"
    That will change onload the url of your picture. You need to have a "neutral" picture, something, in case user has javascript disabled...

    What is your final aim, after all? Looks like more a server-side job than a client-side job
    Last edited by Kor; 01-17-2006 at 06:13 AM.

  12. #12
    Join Date
    Jan 2006
    i have not gone crazy...

    lets try this one then:-

    <script language="JavaScript">
    var vara = "10.jpg"

    function funcelf() {

    what would i put for ???? so that it references the value of vara?

    i havent added other bit of html or function in here to keep question simple.


  13. #13
    Join Date
    Dec 2003
    Bucharest, ROMANIA
    what would i put for ???? so that it references the value of vara?
    Nothin will happend. Let me repeat again. It looks like you don't get how javascript works. To change/create attributes/elements/values you need a method. To make that method work, you need a function. To call that function you need an event. Now, simply inserting that variable as attribute's value woun't work because both method and event are missing. This is javascript, it is not me who invented it...

  14. #14
    Join Date
    Jan 2006
    im seeking a method to wrap around this so it performs how i need.. please be patient with me as not crazy but trying to simulate something as echoed in example i gave.. i didnt include all html etc... send me a private if you can?

  15. #15
    Join Date
    Jan 2006
    Dont worry too much that the stuff has no real explanation of use right now..

    Heres an example of what I was trying to ask... its just a simple bit of html just to explain to you what I am after..

    <script language="JavaScript">
    var vara = "1.JPG"
    var varb = "2.JPG"
    var varc = "3.JPG"

    function funcdo() {
    if(vara == "1.JPG"){
    document.mycell.src='***value of varb**'
    var vara = "***value of varc**'
    <a href="#" onClick="javascript:funcdo();"><img src="**value of varc**" name="mycell" border="0"></a>

    So what I am after is the syntax to put in to the spaces where I have ** becuase I dont know the syntax here to use!!. Want to replace example ***value of varx*** with some javascript correct syntax.

    I want the html also to load so that this visually populates the browser when html launched (as shown above):-

    <img src="**value of varc**" name="mycell" border="0">

    I also know that I am using the names of jpg files in the variables; its just that i want to have it like that so using 1.JPG as opposed to 1 etc... So that it just maps a variable same as a file name for now.. and no real reason why it really has to be a JPG suffix but I prefer..

    I of course would use 3 jpg image files as well..

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.12879 seconds
  • Memory Usage 3,010KB
  • Queries Executed 13 (?)
More Information
Template Usage (37):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (15)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (15)postbit
  • (15)postbit_onlinestatus
  • (15)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates