0

I have a string in java script

<select name="random[]" class="myclass" id="x"> 
     <option>hello</option>
</select>

How can I get the value of id in java script?

And also how can I replace id with a new value in same string?

EDIT : select name is array

Mahender Singh
  • 1,393
  • 3
  • 17
  • 23
  • 4
    Please refrain from parsing HTML with RegEx as it will [drive you į̷̷͚̤̤̖̱̦͍͗̒̈̅̄̎n̨͖͓̹͍͎͔͈̝̲͐ͪ͛̃̄͛ṣ̷̵̞̦ͤ̅̉̋ͪ͑͛ͥ͜a̷̘͖̮͔͎͛̇̏̒͆̆͘n͇͔̤̼͙̩͖̭ͤ͋̉͌͟eͥ͒͆ͧͨ̽͞҉̹͍̳̻͢](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). Use the normal DOM methods (getElementsByName.id ...) – bwoebi Jun 15 '13 at 11:18
  • `document.getElementById('x').id` is so much redundant? lol you can also use `document.getElementsByName('random')[0].id`, changing 0 with the number matching your element in the collection! – Niccolò Campolungo Jun 15 '13 at 11:20
  • can you explain how to use DOM to get id with example? Thnx – Mahender Singh Jun 15 '13 at 11:20
  • You can either use `document.getElementsByTagName('select')[0].id` or `document.getElementsByClassName('myclass')[0].id`, the index is same as before. – Niccolò Campolungo Jun 15 '13 at 11:23
  • Assuming you have full control on how select is created, you can just bite the bullet and use `str.match((/ – raina77ow Jun 15 '13 at 11:25
  • That worked raina Thnx!! can you also help me how can I replace this id value with a new one in same string. – Mahender Singh Jun 15 '13 at 11:35
  • Check my answer, I've added replacing code as well. – raina77ow Jun 16 '13 at 10:38

2 Answers2

1

One possible approach is to create an element from that string, then query against it:

var str = '<select name="random[]" class="myclass" id="x">'  
  +  '<option>hello</option>'
  +  '</select>';

var d = document.createElement('div');
d.innerHTML = str;
var id = d.querySelector('select').id;   // getting the id
d.querySelector('select').id = 'new_x';  // replacing it
str = d.innerHTML;                       // getting the updated string back

I assumed here it's always will be a <SELECT> element which id should be fetched, but you can use whichever selector suits you best. The real point of this approach is creating a temporary DOMElement to use its methods for fetching various attributes and/or nested elements from a HTML string.

raina77ow
  • 103,633
  • 15
  • 192
  • 229
0

you can select an element using this: "document.getElementById("lastName").value" document is one of DOM's core object helps to achieve this.

<!DOCTYPE html>
<html>
    <head>
        <title></title>
<script language="javascript">
var seller = {
firstName:"John Smith";
}
function getAndsetValue(){ 
var ID = document.getElementById("id"); // to get value by ID
document.getElementById("firstName").value = seller.firstName; // To set value to an element with that ID
}
</script>
</head>
<body>

<tr>
                <td>First Name </td>
                <td> 
                    <input type="text" readonly="false" id="id" size="20"/>
                    <input type="text" readonly="true" id="firstName" size="20"/>
                </td>
</tr>
</body>
</html>
Ramesh Sivaraman
  • 1,295
  • 3
  • 19
  • 32