SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast navtej's Avatar
    Join Date
    May 2006
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Accessing js script varaible inhtml ,not using document.write,there should be a way

    Hi all

    I have to access javascript variable in html,to be exact in alt tag of an image .
    I am using C#
    heres the code:

    <script type="text/javascript" language="javascript">

    var strMainKeywords1='<%= strMainKeywords1%>';
    var strMainKeywords2='<%= strMainKeywords2%>';
    var strMainKeywords3='<%= strMainKeywords3%>';
    var strMainKeywords='<%= strMainKeywords%>';


    document.title='<%= title%>';// create title on the fly
    </script>

    ===html====
    <img src="somepsth" alt="here I want to access the js varaible" />

    I am using a user control so cant asign values to serverside variables ,and using c# on seprate file ,so cant do scripting in html.

    I have searched a lot found nothing, some threads but were incomplete
    Last edited by navtej; Jul 29, 2006 at 21:51.

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use document.getElementById to acess the image element, once you done that you can access all the elements attributes, such as alt.

  3. #3
    SitePoint Enthusiast navtej's Avatar
    Join Date
    May 2006
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pepejeria
    Use document.getElementById to acess the image element, once you done that you can access all the elements attributes, such as alt.
    Thanks I have already done using dome heres the code :

    <script type="text/javascript" language="javascript">

    var strMainKeywords1='<%= strMainKeywords1%>';
    var strMainKeywords2='<%= strMainKeywords2%>';
    var strMainKeywords3='<%= strMainKeywords3%>';
    var strMainKeywords='<%= strMainKeywords%>';

    // fill the alt tags of the named img tags mainkeywords1,2,3 dynamically with mainkey words
    function setkeyword()
    {
    //var main = document.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'body');
    //alert("hi");
    var main = document.getElementsByName("MainKeywords");
    var output = main.length;
    for(var i=0;i<output;i++)
    {
    // alert(main[i].id);
    var d = document.getElementById(main[i].id).attributes;
    d.getNamedItem("alt").value =strMainKeywords;
    }
    main = document.getElementsByName("MainKeywords1");
    output = main.length;
    for(var i=0;i<output;i++)
    {
    // alert(main[i].id);
    var d = document.getElementById(main[i].id).attributes;
    d.getNamedItem("alt").value =strMainKeywords1;
    }
    main = document.getElementsByName("MainKeywords2");
    output = main.length;
    for(var i=0;i<output;i++)
    {
    // alert(main[i].id);
    var d = document.getElementById(main[i].id).attributes;
    d.getNamedItem("alt").value =strMainKeywords2;
    }
    main = document.getElementsByName("MainKeywords3");
    output = main.length;
    for(var i=0;i<output;i++)
    {
    // alert(main[i].id);
    var d = document.getElementById(main[i].id).attributes;
    d.getNamedItem("alt").value =strMainKeywords3;
    }
    }

    document.title='<%= title%>';// create title on the fly
    </script>

    but I want to do it other way cause we are to use keywords and they can be in table , or anywhere we want ,like I done above do solvethe problem for images alt, but still if there can be a way to access the bariable directly,that be much finner, there must be a way......


Bookmarks

Posting Permissions

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