Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Actually change HTML to say selected="selected"

    Hey everyone. I'm working on a very complex and lengthy algorithm that deals with a variable number of dropdown boxes.

    I need to be able to save the modified HTML (after JavaScript has run its course) so that whatever changes made by JavaScript are saved and the HTML can be printed as last modified.

    Now, fortunately, JavaScript (in Safari anyway) already removes and adds options using pure HTML, however, when changing the selected index, it doesn't write in the selected parameter.

    Does anyone have any quick workarounds for this?

    Thanks a lot!
    Last edited by Masna; 10-17-2008 at 08:59 AM. Reason: Resolved

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Could you by chance show us the code wrapped around [CODE][/CODE] tags.

    If it's too lengthy, then try to reproduce the problem at a minimal number of codes, so it would be easy to debug.

    Hope that makes sense.
    Last edited by rangana; 10-17-2008 at 08:11 AM. Reason: I mean to say "chance" instead of "change"
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #3
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rangana View Post
    Could you by change show us the code wrapped around [CODE][/CODE] tags.

    If it's too lengthy, then try to reproduce the problem at a minimal number of codes, so it would be easy to debug.

    Hope that makes sense.
    Code:
    theForm.elements[i].selectedIndex = z;
    Trust me, you don't want to see anymore. It's a nightmare (easily one of the most difficult algorithms I've ever programmed).

    Anyway, the above code is what's used to change a dropdown's selected index. I need a way to have JavaScript actually plug in "selected" in the option HTML.

  • #4
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    A basic example:
    Code:
    <script type="text/javascript">
    window.onload=function()
    {
    alert('4th Option will be selected');
    document.getElementById('myselect').selectedIndex=3; // 4th option
    }
    </script>
    <select id="myselect">
    <option selected="selected">1</option>
    <option>2</option>
    <option>3</option>
    <option>4th Option</option>
    <option>5</option>
    </select>
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #5
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    ...or you mean to use setAttribute():
    Code:
    <script type="text/javascript">
    window.onload=function()
    {
    var opt=document.getElementById('myselect'); // Drop down id
    alert('4th Option will be selected');
    opt.options[3].setAttribute('selected','selected'); // 4th option
    }
    </script>
    <select id="myselect">
    <option selected="selected">1</option>
    <option>2</option>
    <option>3</option>
    <option>4th Option</option>
    <option>5</option>
    </select>
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    Masna (10-17-2008)

  • #6
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Will setAttribute actually plug in "selected" for me?

  • #7
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    UPDATE: I tried using setAttribute. It worked, but for some reason it also set a dropdown to selected that shouldn't have been.

    I got it all to work using this:

    Code:
    var outerString = theForm.elements[i].options[z].outerHTML;
    var innerString = theForm.elements[i].options[z].innerHTML;
    var delSplit = outerString.split( ">" + innerString);
    var splitTwo = delSplit[0].split(">");
    var finalString = splitTwo[0] + " selected>" + innerString + "</option>";
    theForm.elements[i].options[z].outerHTML = finalString;

  • #8
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Quote Originally Posted by Masna View Post
    Will setAttribute actually plug in "selected" for me?
    Yes. See testing example:
    Code:
    <script type="text/javascript">
    function findAttr(el,attr)
    {
    var opt=document.getElementById(el),attrFound=false;
    for(var i=0;i<opt.options.length;i++)
    		attrFound=opt.options[i].getAttribute(attr)?true:attrFound;
    alert(attrFound?'"'+attr+'" is found.':'"'+attr+'" cannot be found.');
    }
    function setAttr(el,indx,attr)
    {
    var el=document.getElementById(el),splt=attr.split('=');
    el.options[indx].setAttribute(splt[0],splt[1]);
    }
    </script>
    <select id="myselect">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4th Option</option>
    <option>5</option>
    </select>
    <input type="button" value="Find Attribute" onclick="findAttr('myselect','selected')">
    <input type="button" value="Set Attribute" onclick="setAttr('myselect',3,'selected=selected')>
    For further reading:
    http://developer.mozilla.org/en/DOM/...t.setAttribute
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph


  •  

    Posting Permissions

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